Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
Php 文件排序而不是索引扫描_Php_Mysql_Windows_Mysqli_Query Performance - Fatal编程技术网

Php 文件排序而不是索引扫描

Php 文件排序而不是索引扫描,php,mysql,windows,mysqli,query-performance,Php,Mysql,Windows,Mysqli,Query Performance,我将PHP/MySQL数据库从Unix迁移到Windows。我转储了数据库,直接在Windows上导入,没有任何区别。我使用相同的script/PHP版本 我的桌子是: CREATE TABLE `pximg` ( `ppoc` tinyint unsigned NOT NULL, `file` int unsigned NOT NULL, `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `img` mediumblob,

我将PHP/MySQL数据库从Unix迁移到Windows。我转储了数据库,直接在Windows上导入,没有任何区别。我使用相同的script/PHP版本

我的桌子是:

CREATE TABLE `pximg` (
  `ppoc` tinyint unsigned NOT NULL,
  `file` int unsigned NOT NULL,
  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `img` mediumblob,
  PRIMARY KEY (`ppoc`,`file`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
要复制的我的PHP脚本:

//SQL查询 $queryNoBindings=EXPLAIN SELECT img,ts FROM pximg,其中ppoc=0,订单编号为'file`DESC LIMIT 1; $queryBindings=EXPLAIN SELECT img,ts FROM pximg,其中ppoc=?按`文件`说明限额1订购; $ppoc=0; //连接 $m=新mysqli'127.0.0.1'、$dbData['px'][1]、$dbData['px'][2]、$dbData['px'][3]、$dbData['px'][4]; //不带绑定的查询 $q=$m->query$queryNoBindings; $r1=$q->fetch_arrayMYSQLI_ASSOC; 打印r$r1; //带绑定的查询 $stmt=$m->准备$QueryBinding; $stmt->bind_param'i',$ppoc; $stmt->execute; $r=$stmt->获取结果; $r2=$r->fetch_arrayMYSQLI_ASSOC; 打印r$r2; 在Unix上的结果是

Array
(
    [id] => 1
    [select_type] => SIMPLE
    [table] => pximg
    [partitions] => 
    [type] => ref
    [possible_keys] => PRIMARY
    [key] => PRIMARY
    [key_len] => 1
    [ref] => const
    [rows] => 385758
    [filtered] => 100.00
    [Extra] => Backward index scan
)
Array
(
    [id] => 1
    [select_type] => SIMPLE
    [table] => pximg
    [partitions] => 
    [type] => ref
    [possible_keys] => PRIMARY
    [key] => PRIMARY
    [key_len] => 1
    [ref] => const
    [rows] => 385758
    [filtered] => 100
    [Extra] => Backward index scan
)
Windows上的结果是

Array
(
    [id] => 1
    [select_type] => SIMPLE
    [table] => pximg
    [partitions] => 
    [type] => ref
    [possible_keys] => PRIMARY
    [key] => PRIMARY
    [key_len] => 1
    [ref] => const
    [rows] => 370682
    [filtered] => 100.00
    [Extra] => Backward index scan
)
Array
(
    [id] => 1
    [select_type] => SIMPLE
    [table] => pximg
    [partitions] => 
    [type] => ref
    [possible_keys] => PRIMARY
    [key] => PRIMARY
    [key_len] => 1
    [ref] => const
    [rows] => 370682
    [filtered] => 100
    [Extra] => Using filesort
)
所以区别在于:当我在Windows上使用绑定查询时,它现在使用filesort而不是向后索引扫描,并且需要大约20秒而不是0.02秒

有人知道为什么吗?
或者,如果filesort正常,为什么现在查询速度如此之慢?

看起来新加载的服务器没有使用主键

以下是一些可以尝试的东西:

显示表格像素;在新服务器上显示大容量加载表的定义。主键定义进入新表了吗?如果没有,请这样做

 ALTER TABLE piximg ADD PRIMARY KEY(`ppoc`, `file`);
更改表pximg启用键;大容量加载操作禁用密钥处理的可能性很小

优化表pximg;因为有时候新的大容量加载表的索引统计信息在优化之前是没有意义的

对于此查询的特定情况,稍微更改索引定义,以便在文件列中包含DESC限定符

模式选择“等等,等等,等等…”。。。ORDER BY blah DESC LIMIT 1是一种臭名昭著的性能反模式。你可以试试这个


这将避免MySQL服务器出现混乱,并对其中包含大量大斑点的行进行排序,只会丢弃除一个以外的所有行。

非常感谢您的帮助!1.显示表格显示[索引长度]=>0。。。2号和3号到目前为止没有帮助。5如果查询没有,则结果将使用where;使用索引和它的预期速度-非常感谢!但我仍在研究为什么原始的绑定查询没有使用主键…同一版本的MySQL?请为案例提供EXPLAIN FORMAT=JSON SELECT。成本可能会略有不同。另外,请提供来自..-基数估计可能存在差异。
 PRIMARY KEY (`ppoc`, `file` DESC)
SELECT a.img, a.ts 
  FROM pximg a
  JOIN (
              SELECT ppoc, MAX(file) file
                FROM piximg
               GROUP BY ppoc
       ) b ON a.ppoc = b.ppoc AND a.file = b.file
 WHERE ppoc = <<<whatever>>>