gpt4 book ai didi

mysql - 从其他表中加入最大值的最佳和最佳方式

转载 作者:行者123 更新时间:2023-11-29 00:07:53 25 4
gpt4 key购买 nike

我想找到运行此查询的最佳方式,这里是详细信息,假设以下是我的表及其数据:

-- User Table
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

-- Entries
INSERT INTO `users` VALUES (1,'user_a');
INSERT INTO `users` VALUES (2,'user_b');
INSERT INTO `users` VALUES (3,'user_c');

-- User Log Table
DROP TABLE IF EXISTS `user_log`;
CREATE TABLE `user_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idrel` int(11) NOT NULL,
`event` varchar(20) NOT NULL,
`log` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idrel` (`idrel`,`log`)
) ENGINE=MyISAM;

-- Entries
INSERT INTO `user_log` VALUES (1,1,'log1','2014-01-01 13:00:00');
INSERT INTO `user_log` VALUES (2,1,'log2','2014-01-02 13:00:00');
INSERT INTO `user_log` VALUES (3,2,'log3','2014-01-03 13:00:00');
INSERT INTO `user_log` VALUES (4,2,'log4','2014-01-04 13:00:00');
INSERT INTO `user_log` VALUES (5,3,'log5','2014-01-05 13:00:00');
INSERT INTO `user_log` VALUES (6,3,'log6','2014-01-06 13:00:00');

我想查询加入最新记录事件的用户,我找到了两种方法,但我不知道哪种方法最好(谈论两个表增长时的速度)或者是否有另一种方法,这是我的查询:

-- Query 1
SELECT
u.id,
u.name,
l2.event as last_record
FROM
users AS u
INNER JOIN
(
SELECT
idrel,
MAX(id) as last_id
FROM user_log
GROUP BY
idrel
) AS l1
ON (l1.idrel = u.id)
INNER JOIN
user_log AS l2
ON (l2.id = l1.last_id);

这给了我这个结果:

+----+--------+-------------+
| id | name | last_record |
+----+--------+-------------+
| 1 | user_a | log2 |
| 2 | user_b | log4 |
| 3 | user_c | log6 |
+----+--------+-------------+
3 rows in set (0.00 sec)
-- Query 2
SELECT
u.id,
u.name,
(
SELECT event FROM user_log WHERE idrel = u.id ORDER BY log DESC LIMIT 1
) AS last_record
FROM
users AS u;

结果:

+----+--------+-------------+
| id | name | last_record |
+----+--------+-------------+
| 1 | user_a | log2 |
| 2 | user_b | log4 |
| 3 | user_c | log6 |
+----+--------+-------------+
3 rows in set (0.00 sec)

哪一个可能是最好的方法?还有其他更好的方法吗?

最佳答案

不要那样做,这里有一个更好的方法,首先,在用户表中添加一个字段来存储任何新日志条目的最后一个 ID,然后创建一个触发器,在每个插入的新字段上更新该字段,然后(可选)创建一个 View 以拥有一个完整的“表”以供查询。

-- Add new field to save the modified id
ALTER TABLE `users` ADD `last_mod_id` INT(11) NOT NULL, ADD INDEX (`last_mod_id`);

-- Drop the trigger just in case it exists
DROP TRIGGER IF EXISTS `update_last_id`;

-- Create a trigger to save the last modified id each time a row is created
DELIMITER ;;
CREATE TRIGGER update_last_id
AFTER INSERT ON user_log
FOR EACH ROW
BEGIN
UPDATE `users`
SET `last_mod_id` = NEW.id
WHERE `id` = NEW.idrel;
END;
;;

-- Emtpy your table
TRUNCATE `user_log`;

-- Create all new registries
INSERT INTO `user_log` VALUES (1,1,'log1','2014-01-01 13:00:00');
INSERT INTO `user_log` VALUES (2,1,'log2','2014-01-02 13:00:00');
INSERT INTO `user_log` VALUES (3,2,'log3','2014-01-03 13:00:00');
INSERT INTO `user_log` VALUES (4,2,'log4','2014-01-04 13:00:00');
INSERT INTO `user_log` VALUES (5,3,'log5','2014-01-05 13:00:00');
INSERT INTO `user_log` VALUES (6,3,'log6','2014-01-06 13:00:00');


-- Query
SELECT
u.id,
u.name,
u.last_mod_id,
l.event,
l.log
FROM
`users` as u
INNER JOIN
`user_log` as l
ON (l.id = u.last_mod_id)

-- Drop view if exists
DROP VIEW IF EXISTS `users_log_view`;

-- View
CREATE VIEW `users_log_view` AS
SELECT
u.id,
u.name,
u.last_mod_id,
l.event,
l.log
FROM
`users` as u
INNER JOIN
`user_log` as l
ON (l.id = u.last_mod_id);

-- Query
SELECT * FROM `users_log_view` WHERE `id` = 1;

关于mysql - 从其他表中加入最大值的最佳和最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26661314/

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