MySQL使用强制索引提升查询速度
未使用强制索引时,查询语句如下:
SELECT `orders`.`id`,`orders`.`order_number`,`orders`.`date`,`orders`.`order_status`,`orders`.`sku`,`orders`.`product_name`,`orders`.`quantity`,`orders`.`unit_price`,`orders`.`platform`,`orders`.`tracking_num`,`orders`.`code`,`orders`.`admin_id`,product.id AS product__id,product.sku AS product__sku,product.name AS product__name,product.description AS product__description,product.remark AS product__remark,product.mini_stock AS product__mini_stock,product.sale_price AS product__sale_price,product.cost AS product__cost,product.size AS product__size,product.weight AS product__weight,product.supplier_id AS product__supplier_id,product.booking AS product__booking,product.products_type_id AS product__products_type_id,product.listing AS product__listing,product.base_sku_id AS product__base_sku_id,product.multiple AS product__multiple,product.create_time AS product__create_time,product.update_time AS product__update_time,product.code AS product__code,product.admin_id AS product__admin_id,product.warehouse_id AS product__warehouse_id,product.base_sku_info AS product__base_sku_info,product.is_base_sku AS product__is_base_sku,admin.id AS admin__id,admin.username AS admin__username,admin.nickname AS admin__nickname,admin.password AS admin__password,admin.salt AS admin__salt,admin.avatar AS admin__avatar,admin.email AS admin__email,admin.loginfailure AS admin__loginfailure,admin.logintime AS admin__logintime,admin.loginip AS admin__loginip,admin.createtime AS admin__createtime,admin.updatetime AS admin__updatetime,admin.token AS admin__token,admin.status AS admin__status
FROM `so_orders` `orders`
LEFT JOIN `so_product` `product` ON `orders`.`sku`=`product`.`sku`
LEFT JOIN `so_admin` `admin` ON `orders`.`admin_id`=`admin`.`id`
ORDER BY `date` DESC,`id` DESC
LIMIT 0,10
查询用时:
/* 受影响记录行数: 0 已找到记录行: 10 警告: 0 持续时间 1 查询: 21.641 秒. */
使用强制索引(FORCE INDEX (date))后,查询语句如下:
SELECT `orders`.`id`,`orders`.`order_number`,`orders`.`date`,`orders`.`order_status`,`orders`.`sku`,`orders`.`product_name`,`orders`.`quantity`,`orders`.`unit_price`,`orders`.`platform`,`orders`.`tracking_num`,`orders`.`code`,`orders`.`admin_id`,product.id AS product__id,product.sku AS product__sku,product.name AS product__name,product.description AS product__description,product.remark AS product__remark,product.mini_stock AS product__mini_stock,product.sale_price AS product__sale_price,product.cost AS product__cost,product.size AS product__size,product.weight AS product__weight,product.supplier_id AS product__supplier_id,product.booking AS product__booking,product.products_type_id AS product__products_type_id,product.listing AS product__listing,product.base_sku_id AS product__base_sku_id,product.multiple AS product__multiple,product.create_time AS product__create_time,product.update_time AS product__update_time,product.code AS product__code,product.admin_id AS product__admin_id,product.warehouse_id AS product__warehouse_id,product.base_sku_info AS product__base_sku_info,product.is_base_sku AS product__is_base_sku,admin.id AS admin__id,admin.username AS admin__username,admin.nickname AS admin__nickname,admin.password AS admin__password,admin.salt AS admin__salt,admin.avatar AS admin__avatar,admin.email AS admin__email,admin.loginfailure AS admin__loginfailure,admin.logintime AS admin__logintime,admin.loginip AS admin__loginip,admin.createtime AS admin__createtime,admin.updatetime AS admin__updatetime,admin.token AS admin__token,admin.status AS admin__status
FROM `so_orders` `orders` FORCE INDEX (date)
LEFT JOIN `so_product` `product` ON `orders`.`sku`=`product`.`sku`
LEFT JOIN `so_admin` `admin` ON `orders`.`admin_id`=`admin`.`id`
ORDER BY `date` DESC,`id` DESC
LIMIT 0,10
查询用时:
/* 受影响记录行数: 0 已找到记录行: 10 警告: 0 持续时间 1 查询: 0.015 秒. */
分析语句,可以看到使用到了索引date
thinkPHP中使用:
$this->model->where($where)->with(['product','admin'])->orderRaw('`date` desc,`id` desc')->force('date')->paginate($limit);