索引和优化

 1凡evan 发布于 2023-02-11 22:12

当谈到超越MySQL的基础时,我并不精彩,但是,我正在尝试优化查询:

SELECT DATE_FORMAT(t.completed, '%H') AS hour, t.orderId, t.completed as stamp,
       t.deadline as deadline, t.completedBy as user, p.largeFormat as largeFormat 
    FROM tasks t
    JOIN orders o ON o.id=t.orderId
    JOIN products p ON p.id=o.productId
    WHERE DATE(t.completed) = '2013-09-11'
    AND t.type = 7
    AND t.completedBy IN ('user1', 'user2')
    AND t.suspended = '0'
    AND o.shanleys = 0
    LIMIT 0,100

+----+-------------+-------+--------+----------------------------+-----------+---------+-----------------+-------+-------------+
| id | select_type | table | type   | possible_keys              | key       | key_len | ref             | rows  | Extra       |
+----+-------------+-------+--------+----------------------------+-----------+---------+-----------------+-------+-------------+
|  1 | SIMPLE      | o     | ref    | PRIMARY,productId,shanleys | shanleys  | 2       | const           | 54464 | Using where |
|  1 | SIMPLE      | p     | eq_ref | PRIMARY                    | PRIMARY   | 4       | sfp.o.productId |     1 |             |
|  1 | SIMPLE      | t     | ref    | NewIndex1                  | NewIndex1 | 5       | sfp.o.id        |     6 | Using where |
+----+-------------+-------+--------+----------------------------+-----------+---------+-----------------+-------+-------------+

在添加一些索引之前,它在p表和o表上执行全表扫描.

基本上,我认为MySQL会:

使用where子句限制任务表中的行(应该是没有连接的84行)

然后通过orders表到products表获取一个flag(largeFormat).

我的问题是为什么当MySQL只有84个不同的ID要查找时,它会查找50000多行,有没有办法可以优化查询?

我无法添加新字段或新表.

先感谢您!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有