473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Two methods of running this php query, which is more efficient ?

290 100+
Hi,
I am comparing my update table to the latest data in in my main
data table, so I am selecting the latest data from the main table with this expression:

mday_no is an indexed column.

Expand|Select|Wrap|Line Numbers
  1. $sql_main = "SELECT * FROM cb_main WHERE mid = '$the_id' AND mday_no=(select max(mday_no)";
  2.  
  3.     $result_main = mysql_query($sql_main)
  4.     or die("could not FIND ID in cb_main.". mysql_error()); 
  5.  
However, I realized that I could also do this and I assume I would get the same result.

Expand|Select|Wrap|Line Numbers
  1. $sql_main = "SELECT * FROM cb_main WHERE mid = '$the_id'  ORDER BY  mday_no DESC limit 1";
  2.  
  3.     $result_main = mysql_query($sql_main)
  4.     or die("could not FIND ID in cb_main.". mysql_error()); 
Is one method more efficient that the other?
Jan 14 '10 #1
4 1721
dgreenhouse
250 Expert 100+
The second one is going to run faster whether mid is indexed or not - I assume it is.

In the second query, the query engine only has to do one index lookup whereas in the first, it has a sub-select and an aggregate function.

Run them both with EXPLAIN in a query analyzer:
i.e.
Expand|Select|Wrap|Line Numbers
  1. EXPLAIN SELECT * FROM cb_main WHERE mid = some_number AND mday_no=(select max(mday_no));
  2.  
  3. EXPLAIN SELECT * FROM cb_main WHERE mid = some_number ORDER BY  mday_no DESC limit 1
  4.  
I just realized that the query engine "might just" optimize the first query better than the second in some cases. I'll have to think about that a bit, but as stated, it's best to run it through the query analyzer to determine what the query engine's execution plan will be.
Jan 14 '10 #2
jeddiki
290 100+
The second one is going to run faster whether mid is indexed or not - I assume it is.
Yep, it is, mid and mday_no are indexed.


I don't have a query analyzer :(

Can you suggest where I can get one ?
Jan 14 '10 #3
dgreenhouse
250 Expert 100+
You can use phpMyAdmin (if it's setup and probably would be on a commercial hosting site), the command prompt (assuming SSH [ or telnet - heaven forbid ] access), or a script in PHP. I'll show a little PHP script at the end.

Also, if you can make direct connections to the DB machine(s), you can install a number of tools on your local machine.

Two I use are:

MySQL Workbench (pro or community edition)
http://wb.mysql.com/

- And -

SQLyog (ultimate, enterprise, professional, or community)
http://webyog.com/en/index.php

- There are many more -


// El Cheap-o-php query analyzer...
Expand|Select|Wrap|Line Numbers
  1.  
  2. // Connect…
  3. mysql_connect('localhost','user','pwd');
  4.  
  5. // Add ‘EXPLAIN’ to head of your queries
  6. $sql = 
  7.   'EXPLAIN SELECT a.*
  8.    FROM bytes_help.workorder a
  9.    JOIN bytes_help.workorder b
  10.    ON  b.id = mod(a.id,5)'; // A useless query...
  11.  
  12. // Do the query…
  13. $rs = mysql_query($sql);
  14.  
  15. // Dump the execution plan...
  16. print '<pre>'; // encapsulate with pre's to keep from having to 'view source'
  17. while ($row = mysql_fetch_assoc($rs)) {
  18.   // Fetch into an associative array...
  19.   print_r($row);
  20. }
  21. print '</pre>';
  22.  
  23. // Output...
  24. Array
  25. (
  26.     [id] => 1
  27.     [select_type] => SIMPLE
  28.     [table] => a
  29.     [type] => ALL
  30.     [possible_keys] => 
  31.     [key] => 
  32.     [key_len] => 
  33.     [ref] => 
  34.     [rows] => 101
  35.     [Extra] => 
  36. )
  37. Array
  38. (
  39.     [id] => 1
  40.     [select_type] => SIMPLE
  41.     [table] => b
  42.     [type] => eq_ref
  43.     [possible_keys] => PRIMARY
  44.     [key] => PRIMARY
  45.     [key_len] => 4
  46.     [ref] => func
  47.     [rows] => 1
  48.     [Extra] => Using where; Using index
  49. )
  50.  
  51.  
  52.  
Jan 14 '10 #4
Markus
6,050 Expert 4TB
HeidiSQL is a good one :)
Jan 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
1
by: Jeremy | last post by:
I have built a form that calls queries. I have the first 2 set up as select queries, and the third set up as a make table query. When multiple users are on this application at the same time, they...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
14
by: Alex K. | last post by:
Hi all I'd like to avoid starting the same c# application twice on the same computer. How do I check if it is already running? Thank you
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.