Stop MySQL after first match -


i noticed adding limit 1 @ end of query not decrease execution time. have few thousand records , simple query. how make mysql stop after first match?

for example, these 2 queries both take approximately half second:

select id,content links length(content)<500 order likes  select id,content links length(content)<500 order likes limit 1 

edit: , here's explain results:

id | select_type | table | type possible_keys | key | key_len | ref | rows | 1 | simple | links | | null | null | null | null | 38556 | using where; using filesort 

the difference between 2 queries run time relies on actual data.

there several possible scenarios:

there many records length(content)<500

in case, mysql start scanning table rows (according primary key order since didn't provide order by). there no index use since condition can't indexed. since there relatively many rows length(content)<500, limit query return faster other one.

there no records length(content)<500 again, mysql start scanning table rows, have go through records figure out none of them satisfies condition. again no index can used same reason. in case - 2 queries have same run time.

anything between 2 scenarios have different run times, farther apart have more valid records in table.

edit added order by, answer bit different:

  • if there index on likes column, order use , time time takes first record satisfies condition (if 66% of records do, should faster without limit).

  • if there no index on likes column, order take of time - mysql must scan table records satisfy where, order them likes, , take first one. in case both queries have similar run time (scanning , sorting results longer returning 1 record or many records...)!


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -