English 中文(简体)
adding an index to sql server
原标题:

I have a query that gets run often. its a dynmaic sql query because the sort by changes.

SELECT userID, ROW_NUMBER(OVER created) as rownumber
from users
where
  divisionID = @divisionID and isenrolled=1

the OVER part of the query can be:

  • userid
  • created

Should I create an index for:

  • divisionID + isenrolled
  • divisionID + isenrolled + each_sort_by_option ?

Where should I put indexes for this table?

问题回答

I d start with

CREATE INDEX IX_SOQuestion ON dbo.users (divisionID, isenrolled) INCLUDE (userID, created)
  • The created ranking is unrelated to the WHERE clause, so may as well just INCLUDE it (so it s covered) rather that in the key columns. An internal sort would be needed anyway, so why make the key bigger?

  • Other sort columns could be included too

  • userid is only needed for output, so INCLUDE it

  • perhaps take isenrolled into INCLUDE if it s bit. Only 2 states (OK, 3 with NULL), so kinda pointless to add to the key columns

Start with divisionID + isenrolled + userID as it will always be used





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签