[英]mysql optimize query for paging
pls can anybody help me width this query? 请问有人可以帮我确定这个查询的宽度吗? I cannot find solution for it. 我找不到解决方案。
SELECT *,p.cat_id, cat_name FROM ( SELECT cat_id FROM categories ORDER BY cat_id LIMIT 700000, 10 ) o JOIN categories p ON p.cat_id = o.cat_id ORDER BY p.cat_id
It is very fast on table with 800 000 records, but what I need is to sort data with order by clause and where claue,too - for paging. 它在具有80万条记录的表上非常快,但是我需要的是使用order by子句以及claue -to分页对数据进行排序。 If I use order by it si very slow or result is not ordered correctly> 如果我使用命令非常慢或结果未正确排序>
SELECT *, p.cat_id, cat_name FROM(
SELECT cat_id
FROM categories
LIMIT 700000, 10
) o JOIN categories p
ON p.cat_id = o.cat_id
ORDER BY p.cat_name
page 1
LIMIT 700000,5:
id name
12525525 car
15155151 carpet
1521512i zone
page 2
LIMIT 700005,5
id name
12525525 carefull
15155151 excellent
52151222 drive
I need result: 我需要结果:
page 1 car
carpet
drive
excellent ... etc.
f.e. , this is very slow ofcourse >
SELECT *, p.cat_id, cat_name
FROM (
SELECT cat_id
FROM categories
**ORDER BY cat_name**
LIMIT 700000, 10
) o
JOIN categories p
ON p.cat_id = o.cat_id
ORDER BY p.cat_name
primary key cat_id, autoincrement I have indexes on fields in table 主键cat_id,自动递增我在表中的字段上有索引
Many thanks for help or some ideas 非常感谢您的帮助或一些想法
You didn't need to JOIN
the same table, and you have to do the ORDER BY
in an inner query to be ordered correctly with the WHWRE
clause in the outer one: 您不需要JOIN
同一张表,并且必须在内部查询中执行ORDER BY
才能使用外部查询中的WHWRE
子句正确排序:
SET @rownum = 0;
SELECT t.*, t.rank as TableRank
FROM
(
SELECT *, (@rownum := @rownum + 1) as rank
FROM categories c
ORDER BY c.cat_name
) t
WHERE rank BETWEEN ((@PageNum - 1) * @PageSize + 1)
AND (@PageNum * @PageSize)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.