Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Optimize SQL Query
Message
De
31/07/2009 12:39:11
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01415652
Message ID:
01415687
Vues:
40
>>>To see if the query use the index you could try your query in SSMS and add Actual execution plan in query. then you could see if the SQL Server use Index Seek, Index scan or table scan. Try to write your query that way so SQL to use Index Seek.
>>
>>From my testing it seems I can only get index seek if the fields I want to pull back are also part of the index and I don't have to include all the fields in the index expression or in order
>>
>>
>>  *-- Partial match to index
>>  SELECT holiday FROM table WHERE company = MyComp' AND fiscal = '2009'
>>  *--- Returns back Index seek with corect index
>>
>>  *-- Partial match to index with fields out of order
>>  SELECT holiday FROM table WHERE fiscal = '2009' AND company = MyComp'
>>  *--- Returns back Index seek with correct index
>>
>>  *-- Skipping first field in index expression 
>>  SELECT holiday FROM table WHERE fiscal = '2009' AND holiday = 'Jan 1, 2009'
>>  *--- Returns back Index scan with correct index
>>
>>  *-- Return fields not in index and partial match on index
>>  SELECT * FROM table WHERE company = 'MyComp' AND fiscal = '2009'
>>  *-- Returns Index Scan and doesn't use the correct index uses primary key
>>
>>  *-- Return fields not in index and full match on index
>>  SELECT * FROM table WHERE company = 'MyComp' AND fscal = '2009' AND holiday = 'Jan 1, 2009'
>>  *-- Even with exact match in where still Index Scan with primary key
>>
>
>Use first and last variations as they seem to be the best.

The last Variation didn't use the index and when I dropped the primary key just to see what would happen to the other select statements that where using it. The execution plan went from index scan to table scan. So to me this seems indexes are only good if the fields you are pulling out are in the list of the fields you want to filter down on.
Charles

"The code knows no master." - Chuck Mautz
"Everybody is ignorant, only on different subjects." - Will Rogers
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform