gpt4 book ai didi

php - MySQL:键/值存储查询优化

转载 作者:行者123 更新时间:2023-11-30 22:48:28 25 4
gpt4 key购买 nike

我正在寻求帮助来设置正确的索引(我尝试了太多,现在我有点迷路了),正确的 MySQL 引擎(MyIsam,InnoDB ...) 并帮助处理我的查询(加入,...)。当我想到这些查询应该返回 count(*) 时,我也很头疼。

我每次查询的时间都超过 5 - 10 秒,但我不确定我是否可以为这个大数据库获得更好的时间。

我正在尝试优化这个 MySQL 表:

项目(约 60 万行):

+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| type | varchar(255) | NO | PRI | NULL | |
+-------+------------------+------+-----+---------+----------------+

Items_Relationships(约 100 万行):

+-------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| lft_item_id | int(11) unsigned | NO | PRI | NULL | |
| rgt_item_id | int(11) unsigned | NO | PRI | NULL | |
| rel_type | varchar(255) | NO | PRI | NULL | |
+-------------+------------------+------+-----+---------+-------+

Items_Values(约 400 万行):

+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| item_id | int(11) unsigned | NO | PRI | 0 | |
| name | varchar(255) | YES | MUL | NULL | |
| value | longtext | YES | | NULL | |
| lang | varchar(2) | YES | | NULL | |
+---------+------------------+------+-----+---------+----------------+

我基本上运行这些常见查询:

<强>1。查询 - 值为“状态”> 1 的所有项目:

SELECT `company`.`id` AS `id`,
`company`.`type` AS `type`
FROM `items` AS `company`
INNER JOIN `items_values` AS `value_name` ON (`company`.`id` = `value_name`.`item_id`)
WHERE `company`.`type` = 'company'
AND `value_name`.`name` = 'status'
AND CONVERT(`value_name`.`value`, SIGNED) > 1
GROUP BY `company`.`id`
ORDER BY `company`.`id` DESC
LIMIT 0, 30

<强>2。查询 - 具有与其他项目相关的某些值的所有项目:

SELECT `company`.`id` AS `id`,
`company`.`type` AS `type`
FROM `items` AS `company`
INNER JOIN `items_values` AS `value_status` ON (`value_status`.`item_id` = `company`.`id`)
INNER JOIN `items_relationships` AS `companies_categories` ON (`companies_categories`.`lft_item_id` = `company`.`id`)
INNER JOIN `items_values` AS `category_rgt` ON (`category_rgt`.`item_id` = `companies_categories`.`rgt_item_id`)
WHERE `company`.`type` = 'company'
AND `company`.`type` = 'company'
AND `value_status`.`name` = 'status'
AND CONVERT(`value_status`.`value`, SIGNED) >= 1
AND `category_rgt`.`name` = 'rgt'
AND (CONVERT(category_rgt.value, UNSIGNED) BETWEEN 2805 AND 4222)
AND `companies_categories`.`rel_type` = 'company_category'
GROUP BY `company`.`id`
ORDER BY `company`.`id` DESC LIMIT 10
OFFSET 0

提前致谢!

最佳答案

如果您询问索引,那么您几乎已经为您需要的所有内容编制了索引。

我有一个问题

|姓名 |变种(255) |是 |多个 |空 | |

所以我更愿意设置它以及主键。

关于表结构我只有一个建议。

如果您在

中混合了字符串和数字

|值(value) |长文 |是 | |空 | |

创建另一列 int_value SIGNED 或 unsigned 更好。

并且您还应该将该列设置为索引(只要您需要将该列用作过滤器和/或搜索条件)

并在适用的插入/更新时填写该字段。

此修改将提高查询性能,您不应像此处那样对数百万条记录使用 CAST 和/或 CONVERT:

  AND CONVERT(`value_status`.`value`, SIGNED) >= 1
AND (CONVERT(category_rgt.value, UNSIGNED) BETWEEN 2805 AND 4222)

所以我对结构没有更多的评论。

但我会要求您尝试我的查询,如果它比您的查询更快,就像实验一样。不幸的是,我无法使用任何数据进行调试。如果您提供一些 sqlfiddle 会有很大帮助。

SELECT `company`.`id` AS `id`,
`company`.`type` AS `type`
FROM `items` AS `company`
INNER JOIN (
SELECT
item_id,
FROM items_values
WHERE name = 'status'
AND CONVERT(value, SIGNED) >= 1
) AS value_status
ON value_status.item_id = company.id

INNER JOIN
(
SELECT
lft_item_id
FROM
items_relationships
INNER JOIN (
SELECT
item_id
FROM
items_values
WHERE name = 'rgt'
AND (CONVERT(value, UNSIGNED) BETWEEN 2805 AND 4222)
) AS category_rgt
ON category_rgt.item_id = items_relationships.rgt_item_id
WHERE items_relationships.rel_type = 'company_category'

) as companies_categories
ON (`companies_categories`.`lft_item_id` = `company`.`id`)

WHERE `company`.`type` = 'company'
GROUP BY `company`.`id`
ORDER BY `company`.`id` DESC

LIMIT 10

关于php - MySQL:键/值存储查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28882327/

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