PHP - mysql query execution time | reduce 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);
This question and answers originated from www.stackoverflow.com
Question by (6/17/2011 10:05:54 AM)

Answer

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$$
Answer by

Find More Answers
Related Topics  php  mysql  query  row  execution-time
Related Questions
  • MySql query execution time

    I am using MySQL Workbench on Windows. How do I find out the time taken to execute a query like Select * from employers where employerid > 200 Is there a statement I can enter that returns…
  • MYSQL query execution time

    I have a question for you. I have this database with 250.000 recordings, with 2 text fields each containing up to 300 words. And I want do select all the data that meets some criteria and put it in …
  • Query Execution Time

    I am querying a SQL server 2008 database table having more than 500 million records. The query I fired to move around 10 million records to temp table took 9 hours. Similar query I fired which has b…
  • mysql query execution time - can i get this in milliseconds?

    I'm comparing a few different approaches to getting some data in mysql, directly at the console, using the SQL_NO_CACHE option to make sure mysql keeps running the full query every time. Mysql gives…
  • How to controll Mysql query execution time?

    I have several EVENTS executing every two seconds, I would like to kill those which are running longer than 1 second. Are there way to control query execution time on MySQL 5.5? cheers Arman.…
  • MySQL query execution return value?

    I'm constructing an adapter for MySQL in PHP and I was wondering what should MySQL query execution method return? Should it return a result set? Or the last id / affected rows?
  • oracle query execution time

    I would like to get my guery execution time from Oracle DB. I dont want the time when Oracle finish to print results. In MySQL it is easy to get execution time from shell, but in SQL Plus there are …
  • How to get the execution time of a MySQL query from PHP?

    I execute MySQL queries from PHP and would like to know how time consuming they are. Is there any way to get the execution time of a MySQL query from PHP? I also wonder if the execution time depe…
  • Anyway to Limit MySQL Query Execution time?

    Hi I'm having issues on my server at the minute because some of the MySQL queries are taking a very long time to run, and hogging the resources on the server. I'm already in the process of optimi…
  • Strange mysql query execution time behavior

    hello i'm having a strange execution time behavior over almost same sql queries q1: SELECT `t1`.`id`, `t1`.`key`, `t1`.`module`, `t2`.`value` FROM `translates` AS `t1` LEFT JOIN `translates_…