mysql服务器端分页、排序

icomxhvb  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(435)

你好,我在数据库中有一个表,它包含多个列,超过20个,它包含大约200万条记录
这个表有一个主键:col1,它也是我的表的索引,由于某些原因,该列的数据类型是varchar(200)。
我正在实现服务器端分页、排序和更高版本(过滤)
我在表演上遇到了一个问题,这是我的程序

DELIMITER $$

CREATE DEFINER=`master`@`%` PROCEDURE `spGetData`(
    IN  DisplayStart int ,
    IN  SortCol int ,
    IN  SortDir nvarchar(10),
    IN  Search  nvarchar(255)
)
BEGIN

        DECLARE FirstRec int;
        DECLARE LastRec int;

        SET FirstRec = DisplayStart;

            select 
               col1,col2,col3,col4,col6,col7,col8,col9,col10,col11
               col12,col13,col14,col15,col16,col17,col18,col19,col20
               col21,
                ( select count(*) from myTable) as filtered

            from myTable

order by
case When (@SortCol = 0 and @SortDir = 'asc')  then col1 end asc ,
case When (@SortCol = 0 and @SortDir = 'desc') then col1 end desc ,

case When (@SortCol = 1 and @SortDir = 'asc')  then col2 end asc ,
case When (@SortCol = 1 and @SortDir = 'desc') then col2 end desc ,

case When (@SortCol = 2 and @SortDir = 'asc')  then col3 end asc ,
case When (@SortCol = 2 and @SortDir = 'desc') then col3 end desc ,

case When (@SortCol = 3 and @SortDir = 'asc')  then col4 end asc ,
case When (@SortCol = 3 and @SortDir = 'desc') then col4 end desc ,

case When (@SortCol = 4 and @SortDir = 'asc')  then col5 end asc ,
case When (@SortCol = 4 and @SortDir = 'desc') then col5 end desc ,

case When (@SortCol = 5 and @SortDir = 'asc')  then col6 end asc ,
case When (@SortCol = 5 and @SortDir = 'desc') then col6 end desc ,

case When (@SortCol = 6 and @SortDir = 'asc')  then col7 end asc ,
case When (@SortCol = 6 and @SortDir = 'desc') then col7 end desc ,

case When (@SortCol = 7 and @SortDir = 'asc')  then col8 end asc ,
case When (@SortCol = 7 and @SortDir = 'desc') then col8 end desc ,

case When (@SortCol = 8 and @SortDir = 'asc')  then col9 end asc ,
case When (@SortCol = 8 and @SortDir = 'desc') then col9 end desc ,

case When (@SortCol = 9 and @SortDir = 'asc')  then col10 end asc ,
case When (@SortCol = 9 and @SortDir = 'desc') then col10 end desc ,

case When (@SortCol = 10 and @SortDir = 'asc')  then col11 end asc ,
case When (@SortCol = 10 and @SortDir = 'desc') then col11 end desc ,

case When (@SortCol = 11 and @SortDir = 'asc')  then col12 end asc ,
case When (@SortCol = 11 and @SortDir = 'desc') then col12 end desc ,

case When (@SortCol = 12 and @SortDir = 'asc')  then col13 end asc ,
case When (@SortCol = 12 and @SortDir = 'desc') then col13 end desc ,

case When (@SortCol = 13 and @SortDir = 'asc')  then col14 end asc ,
case When (@SortCol = 13 and @SortDir = 'desc') then col14 end desc ,

case When (@SortCol = 14 and @SortDir = 'asc')  then col15 end asc ,
case When (@SortCol = 14 and @SortDir = 'desc') then col15 end desc ,

case When (@SortCol = 15 and @SortDir = 'asc')  then col16 end asc ,
case When (@SortCol = 15 and @SortDir = 'desc') then col16 end desc ,

case When (@SortCol = 16 and @SortDir = 'asc')  then col17 end asc ,
case When (@SortCol = 16 and @SortDir = 'desc') then col17 end desc ,

case When (@SortCol = 17 and @SortDir = 'asc')  then col18 end asc ,
case When (@SortCol = 17 and @SortDir = 'desc') then col18 end desc ,

case When (@SortCol = 18 and @SortDir = 'asc')  then col19 end asc ,
case When (@SortCol = 18 and @SortDir = 'desc') then col19 end desc ,

case When (@SortCol = 19 and @SortDir = 'asc')  then col20 end asc ,
case When (@SortCol = 19 and @SortDir = 'desc') then col20 end desc ,

case When (@SortCol = 20 and @SortDir = 'asc')  then col21 end asc ,
case When (@SortCol = 20 and @SortDir = 'desc') then col21 end desc ,

                limit FirstRec,10;

    END

查询速度非常慢,并且会产生缓冲区大小问题。如果我删除ORDERBY子句,它会变得非常快。
所以我的问题是
1-如何增强此查询并快速对数百万行进行排序?
2-稍后我将使用where子句对多列应用筛选,如何避免任何性能问题?

eivgtgni

eivgtgni1#

这个 ORDER BY 应该构造(concat、prepare、execute、deallocate)而不是拥有不能使用任何索引的庞大表达式。
有可能,但不现实,添加 INDEX(col0), INDEX(col1), ... 所以准备好的陈述在所有21个案例中都是快速的。选择重要的,不要把剩下的提供给最终用户。
不要用偏移量分页;而是“记住你在哪里停下来了”。
过滤装置在哪里?这可能会干扰我已经给你的提示。 VARCHAR(200) 通常是不明智的 PRIMARY KEY .
用户能否在两列上指定排序?筛选日期范围?其他事情(请提供真实的 SHOW CREATE TABLE 如果你想了解更多细节。)
你“规范化”过任何列吗?也就是说,将经常重复的大值移到其他表中。

相关问题