Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seek & Scan in SQL Server

After googling i came to know that Index seek is better than scan.

How can I write the query that will yield to seek instead of scan. I am trying to find this in google but as of now no luck.

Any simple example with explanation will be appreciated.

Thanks

like image 989
priyanka.sarkar Avatar asked Dec 30 '22 12:12

priyanka.sarkar


1 Answers

  1. Search by the primary key column(s)
  2. Search by column(s) with index(es) on them

An index is a data structure that improves the speed of data retrieval operations on a database table. Most dbs automatically create an index when a primary key is defined for a table. SQL Server creates an index for primary key (composite or otherwise) as a "clustered index", but it doesn't have to be the primary key - it can be other columns.

NOTE:

  • LIKE '%'+ criteria +'%' will not use an index; LIKE criteria +'%' will

Related reading:

  • SQL SERVER – Index Seek vs. Index Scan
  • Index
  • Which is better: Bookmark/Key Lookup or Index Scan
like image 137
OMG Ponies Avatar answered Jan 14 '23 07:01

OMG Ponies