我正在处理具有数百万数据的应用程序,我需要列出页面中的所有数据。起初我在一个查询中获取所有数据,并且在client side
中完成了分页。但是这需要将近15分钟来加载整套。所以我更改了每个请求获取10行的代码,并在server side
上完成了分页。但表现仍然不尽如人意。那么所有事情应该做些什么来快速获取数据或者处理大量数据的最佳方式。
我的获取数据的查询:
更新:
SELECT w.work_order_id,
(SELECT CONCAT(user_fname, ' ', user_lname) FROM users WHERE user_id = w.created_by) AS created_by,
CASE w.assignee WHEN 0 THEN 'All' WHEN -1 THEN 'Unassigned' ELSE CONCAT(u.user_fname, ' ', u.user_lname) END AS assignee
FROM FiveVan_work_orders w
LEFT JOIN users u ON (u.user_id = w.assignee)
WHERE ( w.work_order_status != 'Deleted' && w.work_order_status != 'Closed') ORDER BY w.created_on DESC LIMIT 0,10;
我已经为页面创建了索引,这是解释查询的结果
+----+--------------------+-------+--------+-------------------+---------------+---------+-------------------------------+--------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+--------+-------------------+---------------+---------+-------------------------------+--------+------------------------------------------+
| 1 | PRIMARY | w | index | work_order_status | work_order_id | 790 | NULL | 340319 | Using where; Using index; Using filesort |
| 1 | PRIMARY | u | eq_ref | PRIMARY | PRIMARY | 4 | fivevan_loadtest.w.assignee | 1 | NULL |
| 2 | DEPENDENT SUBQUERY | users | eq_ref | PRIMARY | PRIMARY | 4 | fivevan_loadtest.w.created_by | 1 | NULL |
+----+--------------------+-------+--------+-------------------+---------------+---------+-------------------------------+--------+------------------------------------------+
答案 0 :(得分:0)
WHERE ( w.work_order_status != 'Deleted' &&
w.work_order_status != 'Closed')
ORDER BY w.created_on DESC
如果只有 一个work_order_status
的值,则将其更改为
WHERE w.work_order_status = 'Open'
ORDER BY w.created_on DESC
并添加
INDEX(work_order_status, created_on)
如果有多个其他值,这可能会起作用(不太好):
INDEX(created_on)
为了获得更好的性能,您需要“记住中断的位置”,而不是使用OFFSET
。我在my pagination blog中讨论过这个问题。