Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: View against Table - are queries against the View still using Table Indexes?

Tags:

sql

tsql

view

I'm using a view ("UsersActive") against my table ("Users"). The view has only one filter, it checks if DateTime Users.DeletedOn is NULL; it basically contains all Users that are not deleted.

If I now execute Linq queries against the view instead of the table, will they still use the table indexes or do I need to create special indexes for the view? In my understanding the View is nothing else but a predefined query and should work just as if I was querying this directly:

SELECT * FROM Users WHERE DeletedON = NULL

Is my assumption that the underlying table's indexes will still be used correct?

like image 313
Alex Avatar asked Mar 01 '23 16:03

Alex


1 Answers

Most of the time, SQL Server Query Optimizer is smart enough to use the indexes on the base table. You can see this by looking at the query plans.

If you have enterprise edition of the SQL Server you can also use indexed views.

like image 161
no_one Avatar answered Mar 03 '23 14:03

no_one