mysql查询执行时间|减少查询

发布于 2024-11-16 01:25:30 字数 1420 浏览 4 评论 0原文

我有一个查询可以让我更改用户顺序。

这是我的查询:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

战皆罪 2024-11-23 01:25:30

我想知道执行该查询是否会花费很多时间,

呃...您的意思是想知道该查询是否会花费很多时间来执行?当然超出了它的需要 - 但很大程度上取决于数据库的结构、数据库中的数据以及您对“大量时间”的定义。

查询是一团糟。撇开查询不必要的复杂性不谈,它也是错误的 - 您使用 LIMIT 检索列表中的下一个条目,而不定义该列表应如何排序。

看起来应该改变列表的顺序。更明智的是(假设 orderID 是唯一的):

 DELIMITER $

 CREATE PROCEDURE bubble_up(IN p_selected INTEGER)
 BEGIN
     DECLARE l_selected_seq INTEGER;
     DECLARE l_replaced_seq INTEGER;

     SELECT orderID into l_selected_seq 
     FROM test 
     WHERE id=p_selected;

     IF (l_selected>1) THEN
        SELECT orderID INTO l_replaced_seq
        WHERE orderID>l_selected_seq
        ORDER BY orderID
        LIMIT 0,1;

        -- swap the values
        UPDATE test SET orderID = IF(orderID=l_replaced_seq, 
                    l_selected_seq, l_replaced_seq)
        WHERE orderID IN (l_replaced_seq, l_selected_seq);
     END IF
 END$

I want to know if it won't took a lot of time to execute that query,

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):

 DELIMITER $

 CREATE PROCEDURE bubble_up(IN p_selected INTEGER)
 BEGIN
     DECLARE l_selected_seq INTEGER;
     DECLARE l_replaced_seq INTEGER;

     SELECT orderID into l_selected_seq 
     FROM test 
     WHERE id=p_selected;

     IF (l_selected>1) THEN
        SELECT orderID INTO l_replaced_seq
        WHERE orderID>l_selected_seq
        ORDER BY orderID
        LIMIT 0,1;

        -- swap the values
        UPDATE test SET orderID = IF(orderID=l_replaced_seq, 
                    l_selected_seq, l_replaced_seq)
        WHERE orderID IN (l_replaced_seq, l_selected_seq);
     END IF
 END$
ˉ厌 2024-11-23 01:25:30

查询的逻辑是不是有点太多了?我会使用一些代码来实现结果...

如果出于某种原因被迫进行复杂的 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 ...

巴黎夜雨 2024-11-23 01:25:30

我看到了一些问题,但我认为最大的问题是您正在更新所有表,而我发现您实际上只需要更新特定行。您应该在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文