gpt4 book ai didi

mysql - 加快 mysql 查询?

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:53 25 4
gpt4 key购买 nike

查询使用cakephp调用数据库,这里是查询:

find('all', array(
'conditions' => array(
'OR' => array(
'PdfFolder.is_global' => true,
'PdfFoldersUser.user_id' => $this->Auth->user('id'))
),
'recursive' => 0,
'order' => array('PdfFolder.is_global DESC', 'PdfFolder.name')

pdffolder 表大约有 2100 条,而包含 pdf_folder_id 和 user_id 的关系表包含近 30000 条记录。此查询有时会在某些时间运行长达 35 秒。我没有设置代码,我也不是数据库专家,这只是我作为开发人员的问题。

看起来每个表的 id 都设置为主键,我看到的所有索引都是这样。

感谢您的任何建议。

编辑

表结构

用户

| id (index) | role_id | username | password | email |

pdf_文件夹

| id (index) | name | date_created | is_global(bool) |

pdf_folders_users

| id (index) | folder_id | user_id (now an index) |

pdf

| id (index) | parent_id | pdf_folder_id | ...more file information

最佳答案

首先在 user_id 字段上添加索引 -

CREATE INDEX FK_user_id ON PdfFoldersUser (user_id)

如果您发布每个相关表的完整表结构,将会有所帮助。

关于mysql - 加快 mysql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9295170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com