Query selection checks more rows than rows existing in the table.

The content of the database contains only 9100 articles. But for now, this request

SELECT * 
FROM `ABC` 
WHERE st=6 AND publish_on <= '2018-02-01' 
ORDER BY rand() LIMIT 5

Query_time: 1.043072 Lock_time: 0.000081 Rows_sent: 5 Rows_examined: 19354

But:

Select count(*) from ABC;

it returns:

9100

Why did he view 19354 lines?

+4
source share
2 answers

You can see in this how the function works rand().

On this link you can see how it works ORDER BY:

  • Read the lines that match the WHERE clause as before.
  • , , , .
  • , , a .

, 19354 ( , ) - order by rand(), rand() ( ), dbms . , , , , dbms 1 .

, ORDER BY rand() LIMIT 5 , 9100 , 5 .

+2
Rows_examined: 19354 

, . 19354, .

EXPLAIN SELECT - , , , , , MySQL . , , .

+1

Source: https://habr.com/ru/post/1524825/


All Articles