Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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
使用order by进行Mysql查询。奇怪的执行计划_Mysql_Explain - Fatal编程技术网

使用order by进行Mysql查询。奇怪的执行计划

使用order by进行Mysql查询。奇怪的执行计划,mysql,explain,Mysql,Explain,我有三张桌子: Product (about 700000 rows) ProductId int(11) AI PK ManufacturerId int(11) FK Name varchar(256) Description text SKU varchar(64) Code varchar(64) ArtId int(11) StockStateId int(2) FK Quantity int(11) QuantityText varchar(61) Price dec

我有三张桌子:

Product (about 700000 rows)
ProductId int(11) AI PK 
ManufacturerId int(11) FK
Name varchar(256) 
Description text 
SKU varchar(64) 
Code varchar(64) 
ArtId int(11) 
StockStateId int(2) FK
Quantity int(11) 
QuantityText varchar(61) 
Price decimal(12,2) 
CurrencyId int(2) 
AutoImport bit(1) 
ImpactOnBalance bit(1) 
HasPhoto bit(1) 
HasParams bit(1)

StockState (3 rows)
StockStateId int(2) AI PK 
Name varchar(64)

Manufacturer (about 200 rows)
ManufacturerId int(11) AI PK 
Name varchar(64) 
Description text 
SortOrder int(11)
这是我的问题

select
     p.ProductId
    ,p.Name
    ,p.Quantity
    ,p.QuantityText
    ,m.ManufacturerId
    ,m.Name as ManufacturerName
    ,ss.StockStateId
    ,ss.Name as StockStateName
from Product p
inner join Manufacturer m on m.ManufacturerId = p.ManufacturerId
inner join StockState ss on ss.StockStateId = p.StockStateId
order by p.ProductId asc
limit 1000, 25
我无法理解为什么mysql不使用正确的索引(需要~10秒才能得到结果)。执行计划是这样的 . 我可以强制mysql使用主索引

from Product p force index (primary)
它会将性能提高到0.015秒,但我将在SP中使用此查询,其中顺序取决于输入参数。所以我添加了虚拟案例条件

set @order = '';
select
     p.ProductId
    ,p.Name
    ,p.Quantity
    ,p.QuantityText
    ,m.ManufacturerId
    ,m.Name as ManufacturerName
    ,ss.StockStateId
    ,ss.Name as StockStateName
    from Product p force index (primary)
inner join Manufacturer m on m.ManufacturerId = p.ManufacturerId
inner join StockState ss on ss.StockStateId = p.StockStateId
order by case when @order = '' then p.ProductId end asc
limit 1000, 25
这个查询应该具有与前一个查询相同的执行计划(按同一列排序,即PK),但是没有,我有filesort。


为什么会这样?有人能帮我修一下吗?(提高查询性能)r

我认为您应该试试这个,有一个左连接概念,等等

set @order = '';
select
     p.ProductId
    ,p.Name
    ,p.Quantity
    ,p.QuantityText
    ,m.ManufacturerId
    ,m.Name as ManufacturerName
    ,ss.StockStateId
    ,ss.Name as StockStateName
    from Product p force index (primary)
//this line what I mean 
inner join Manufacturer m on p.ManufacturerId = m.ManufacturerId
//also this line what I mean
inner join StockState ss on p.StockStateId = ss.StockStateId
order by case when @order = '' then p.ProductId end asc
limit 1000, 25
因为您必须先检查产品中的行,然后加入制造商

若您首先检查制造商,若产品中不存在制造商中的行,则会出现问题(浪费时间)