Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Jak ulepszyć klauzulę Limit w MySQL

Spróbuj tego:

SELECT post_id
    FROM posts
    ORDER BY post_id DESC
    LIMIT 0, 10;

Paginacja przez LIMIT i tak nie ma sensu bez zamawiania, a to powinno rozwiązać Twój problem.

mysql> explain select * from foo;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | foo   | index | NULL          | PRIMARY | 4       | NULL |   20 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from foo limit 0, 10;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | foo   | index | NULL          | PRIMARY | 4       | NULL |   20 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from foo order by id desc limit 0, 10;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | foo   | index | NULL          | PRIMARY | 4       | NULL |   10 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

Jeśli chodzi o Twoje ostatnie komentarze dotyczące dołączenia do komentarzy. Czy masz indeks na comment(post_id) ? z moimi danymi testowymi otrzymuję następujące wyniki:

mysql> alter table comments add index pi (post_id);
Query OK, 0 rows affected (0.15 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select c.id from  comments c inner join (select id from posts o order by id  limit 0, 10) p on c.post_id = p.id;
+----+-------------+------------+-------+---------------+---------+---------+------+------+--------------------------+
| id | select_type | table      | type  | possible_keys | key     | key_len | ref  | rows | Extra                    |
+----+-------------+------------+-------+---------------+---------+---------+------+------+--------------------------+
|  1 | PRIMARY     | <derived2> | ALL   | NULL          | NULL    | NULL    | NULL |   10 |                          |
|  1 | PRIMARY     | c          | ref   | pi            | pi      | 5       | p.id |    4 | Using where; Using index |
|  2 | DERIVED     | o          | index | NULL          | PRIMARY | 4       | NULL |   10 | Using index              |
+----+-------------+------------+-------+---------------+---------+---------+------+------+--------------------------+

i odniesienie do rozmiaru tabeli:

mysql> select count(*) from posts;
+----------+
| count(*) |
+----------+
|    15021 |
+----------+
1 row in set (0.01 sec)

mysql> select count(*) from comments;
+----------+
| count(*) |
+----------+
|     1000 |
+----------+
1 row in set (0.00 sec)



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. zwróć jedną wartość z bazy danych za pomocą mysql php pdo

  2. Czy istnieje różnica w wydajności między BETWEEN i IN w MySQL lub ogólnie w SQL?

  3. MySQL GROUP BY nie działa po migracji do SQL Server 2012

  4. Wyświetlanie danych mysql w tabeli html PHP

  5. Błąd zapytania SQL LIKE — błąd krytyczny w przygotowanej instrukcji