Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

Does WHERE condition scan the entire table?

In mysql, writing a query with WHERE condition on a table with thousands of records, can it slow down the server or the program?

Eg : SELECT * FROM table_name WHERE id = 5;

Upvotes: 1

Views: 1320

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269973

By "default" it would.

One exception is when you have an index on a column. Typically, a column called id would be a primary key -- which generates an index. In that case, the query would be very fast.

Another exception would be if there table were partitioned by id. Then only one partition would be read.

And, if the query had a limit 1, then the query would read the table until it found a match, then it would stop.

So, a where clause does not always scan the entire table.

Upvotes: 4

Related Questions