Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql服务器端分页、排序_Mysql_Performance_Database Performance - Fatal编程技术网

Mysql服务器端分页、排序

Mysql服务器端分页、排序,mysql,performance,database-performance,Mysql,Performance,Database Performance,您好,我在数据库中有一个表,它包含多个列,超过20个,它包含大约200万条记录 该表有一个主键:Col1,它也是我的表的索引 ,由于某些原因,该列的数据类型为varchar(200) 我正在实现服务器端分页、排序和更高版本(过滤) 我面临一个性能问题,以下是我的程序 DELIMITER $$ CREATE DEFINER=`master`@`%` PROCEDURE `spGetData`( IN DisplayStart int , IN SortCol int ,

您好,我在数据库中有一个表,它包含多个列,超过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子句对多列应用筛选,如何避免任何性能问题

  • 应该构造(concat、prepare、execute、deallocate)orderby,而不是使用无法使用任何索引的庞大表达式
  • 可以添加
    索引(col0)、索引(col1),…
    ,这样在所有21种情况下准备好的语句都会很快。选择重要的,不要向最终用户提供其余的
  • );而是“记住你从哪里停下来的”
  • 过滤器在哪里?这可能会干扰我已经给你的提示
  • VARCHAR(200)
    对于
    主键来说通常是不明智的
  • 用户能否在两列上指定排序?过滤日期范围?其他事情?(如果您想了解更多详细信息,请提供real
    SHOW CREATE TABLE
  • 您是否“规范化”了任何列?也就是说,将经常重复的大值移动到其他表中

hi@rick,应该构造orderby(concat、prepare、execute、deallocate),而不是使用无法使用任何索引的庞大表达式。你能澄清一下吗?搜索“mysql程序concat prepare”,你可能会发现很多例子。