douzhui8531 2018-06-18 23:26
浏览 160

Mysql查询选择需要很多时间

I have a site in Laravel that does the following SQL queries, and combined they take like 1.8 seconds. The execution time is taken from the profiler toolbar

675.81ms    
SELECT definitions.*, terms.term, GROUP_CONCAT(DISTINCT(tags.name) SEPARATOR ",") AS tags FROM definitions INNER JOIN terms ON terms.id = definitions.term_id LEFT JOIN definition_tag ON definitions.id = definition_tag.definition_id LEFT JOIN tags ON tags.id = definition_tag.tag_id WHERE approved = 1 GROUP BY definitions.id ORDER BY id ASC LIMIT 2
0.21ms  
SELECT * FROM `settings` WHERE `name` = 'no_homepage_random' LIMIT 1
636.11ms    
SELECT definitions.*, terms.term, GROUP_CONCAT(DISTINCT(tags.name) SEPARATOR ",") AS tags FROM definitions INNER JOIN terms ON terms.id = definitions.term_id LEFT JOIN definition_tag ON definitions.id = definition_tag.definition_id LEFT JOIN tags ON tags.id = definition_tag.tag_id WHERE approved = 1 GROUP BY definitions.id ORDER BY RAND() LIMIT 1
628.68ms    
SELECT definitions.*, terms.term, GROUP_CONCAT(DISTINCT(tags.name) SEPARATOR ",") AS tags FROM definitions INNER JOIN terms ON terms.id = definitions.term_id LEFT JOIN definition_tag ON definitions.id = definition_tag.definition_id LEFT JOIN tags ON tags.id = definition_tag.tag_id WHERE approved = 1 GROUP BY definitions.id ORDER BY id DESC LIMIT 3

I want to speed up sql queries in general, and also im kinda confused why it only takes 0.0049 seg running the same queries in phpmyadmin. Website is in a shared hosting. What can I do to improve?

  • 写回答

2条回答 默认 最新

  • doubeng9407 2018-06-19 09:42
    关注

    May be what makes your request takes lots of time is the inner and left join command: In one of the tables : definitions,terms,definition_tag and tags there is a redundancy in the field and using the inner and left join will make your request takes a lot of time.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题