mysql查询执行时间|减少查询
我有一个查询可以让我更改用户顺序。
这是我的查询:
update test set orderID =
case orderID
when (select orderID from ( select * from test where orderID > ( select orderID from test where id = 'id I want to move up' ) limit 1) as nextOrderID ) then ( select orderID from ( select * from test where id = 'id I want to move up') as nextOrderID )
when ( select orderID from ( select * from test where id = 'id I want to move up' ) as nextOrderID ) then (select orderID from ( select * from test where orderID > ( select orderID from test where id = 'id I want to move up' ) limit 1) as nextOrderID )
else
orderID
end
我想知道执行该查询是否不会花费大量时间,如果会,如何使其加载速度更快,是否可以减少该查询?
更新:
我稍微编辑了代码,所以现在我认为它会更快地执行查询。 这是代码的一部分:
$query = "
SELECT (
SELECT orderID
FROM test WHERE id = 'user id that i want to move up'
) AS user_order,
(
SELECT orderID
FROM test WHERE orderID > user_order
ORDER BY orderID
LIMIT 0,1
) AS nextUser_order
";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
$query = "
UPDATE test SET orderID = IF(orderID='{$data[nextUser_order]}',
'{$data[user_order]}', '{$data[nextUser_order]}')
WHERE orderID IN ('{$data[nextUser_order]}', '{$data[user_order]}');
";
$result = mysql_query($query);
I have a query which let me to change users order.
here is my query:
update test set orderID =
case orderID
when (select orderID from ( select * from test where orderID > ( select orderID from test where id = 'id I want to move up' ) limit 1) as nextOrderID ) then ( select orderID from ( select * from test where id = 'id I want to move up') as nextOrderID )
when ( select orderID from ( select * from test where id = 'id I want to move up' ) as nextOrderID ) then (select orderID from ( select * from test where orderID > ( select orderID from test where id = 'id I want to move up' ) limit 1) as nextOrderID )
else
orderID
end
I want to know if it won't took a lot of time to execute that query, and if it will , how to make it load faster, and is it possible to reduce that query?
Update:
i edited the code a bit so now i think it will execute query faster..
here is a part of code :
$query = "
SELECT (
SELECT orderID
FROM test WHERE id = 'user id that i want to move up'
) AS user_order,
(
SELECT orderID
FROM test WHERE orderID > user_order
ORDER BY orderID
LIMIT 0,1
) AS nextUser_order
";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
$query = "
UPDATE test SET orderID = IF(orderID='{$data[nextUser_order]}',
'{$data[user_order]}', '{$data[nextUser_order]}')
WHERE orderID IN ('{$data[nextUser_order]}', '{$data[user_order]}');
";
$result = mysql_query($query);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呃...您的意思是想知道该查询是否会花费很多时间来执行?当然超出了它的需要 - 但很大程度上取决于数据库的结构、数据库中的数据以及您对“大量时间”的定义。
查询是一团糟。撇开查询不必要的复杂性不谈,它也是错误的 - 您使用 LIMIT 检索列表中的下一个条目,而不定义该列表应如何排序。
看起来应该改变列表的顺序。更明智的是(假设 orderID 是唯一的):
er...do you mean you want to know if the query will take a lot of time to execute? Certainly more than it needs to - but a lot depends on the structure of your database, the data within it, and your definition of 'a lot of time'.
The query is a horrendous mess. Leaving aside the unnecessary complexity of the query, it is also wrong - you are using LIMIT to retrieve the next entry in a list without defining how that list should be sorted.
It looks like it is supposed to change the ordering of a list. It'd be far more sensible to (this assumes orderID is unique):
查询的逻辑是不是有点太多了?我会使用一些代码来实现结果...
如果出于某种原因被迫进行复杂的 mysql 查询,您可以发布表的 desc 吗?首先,我会尝试解释选择,以确保我拥有必要的索引......
Isn't a bit too much logic for a query ? I'd use some code to achieve the result ...
If there's some reason for being forced to a complex mysql query can you post the desc of the table ? First thing I would try an explain of the selects in order to assure I have the necessary indexes ...
我看到了一些问题,但我认为最大的问题是您正在更新所有表,而我发现您实际上只需要更新特定行。您应该在 UPDATE 语句中添加 WHERE 子句,以防止锁定(和更新)不需要更新的行。
I see some issues, but I think the biggest one is that you are updating all the table while I see that you actually need to update particular row(s) only. You should add WHERE clause in your UPDATE statement to prevent the rows from locking (and updating) which you do not need to update.