![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
This is for a message board, where I have 2 tables: "subjects", and "posts". Which is going to be faster and more efficient? // Two queries: SELECT SQL_CACHE lastmodified, subject, firstname, lastname, shortdesc FROM subjects WHERE id=35481; SELECT SQL_CACHE * FROM posts WHERE id=35481 ORDER BY postdate ASC LIMIT 21 OFFSET 0; // Search 2 tables in 1 query SELECT SQL_CACHE subjects.lastmodified, subjects.subject, subjects.firstname, subjects.lastname, subjects.shortdesc, posts.postdate, posts.username, posts.comment FROM subjects, posts WHERE subjects.id=35481 AND posts.id=35481 ORDER BY postdate ASC LIMIT 21 OFFSET 0 I'm not sure how to test the timing on this. If I do 2 queries, do I add the query times together to find the total, and then compare that to the query time of the one that's 1 query to see which is faster? |
#3
| |||
| |||
|
|
Did you try them and find out? There are way too many variables to tell, i.e. database size, cardinality, defined indexes, buffer sizes, system load, ... each installation can be different. |
|
In general, it's not a good idea to use SELECT *. It can cause hard-to-find problems in your programs should the database design change. Additionally, if you add columns (i.e. a 2Mb BLOB), you will fetch unnecessary data which you will never use. You should almost always specify the columns. You also don't need SQL_CACHE; MySQL will automatically cache results as possible. |
|
But it looks like you're falling into the trap of premature optimization. Rather than worry about that now, define your database and programs well and test. Then if you have a performance problem, determine the cause of that problem and fix it. |
#4
| |||
| |||
|
|
On Tuesday, December 6, 2011 5:57:21 AM UTC-5, Jerry Stuckle wrote: Did you try them and find out? There are way too many variables to tell, i.e. database size, cardinality, defined indexes, buffer sizes, system load, ... each installation can be different. That's the thing; I'm not sure HOW to determine which is faster. I know that the speed of either of the first queries is about the same as the speed in searching two tables in one query (ie, 0.007 to 0.008), but does that mean that running two separate queries would be twice as long as the one? In general, it's not a good idea to use SELECT *. It can cause hard-to-find problems in your programs should the database design change. Additionally, if you add columns (i.e. a 2Mb BLOB), you will fetch unnecessary data which you will never use. You should almost always specify the columns. You also don't need SQL_CACHE; MySQL will automatically cache results as possible. That makes sense, thanks for the note. But it looks like you're falling into the trap of premature optimization. Rather than worry about that now, define your database and programs well and test. Then if you have a performance problem, determine the cause of that problem and fix it. The program has been running 2 queries (the first example) for about a year without any problems. But right now, I'm going through and rebuilding the backend to be a little easier to moderate, and while doing so, I figure that this is a good time to change the query if doing the second example would be better. So, I don't think it's really premature optimization, so much as unnecessarily playing around :-) Besides, I think of it more as a knowledge-gaining experience. I know that I won't physically see a difference that's just microseconds, but working with more and more complex queries helps me to understand the process behind it a little better. |
#5
| |||
| |||
|
|
On Tuesday, December 6, 2011 5:57:21 AM UTC-5, Jerry Stuckle wrote: Did you try them and find out? There are way too many variables to tell, i.e. database size, cardinality, defined indexes, buffer sizes, system load, ... each installation can be different. That's the thing; I'm not sure HOW to determine which is faster. I know that the speed of either of the first queries is about the same as the speed in searching two tables in one query (ie, 0.007 to 0.008), but does that mean that running two separate queries would be twice as long as the one? |
#6
| |||
| |||
|
|
This is for a message board, where I have 2 tables: "subjects", and "posts". Which is going to be faster and more efficient? |
|
// Two queries: SELECT SQL_CACHE lastmodified, subject, firstname, lastname, shortdesc FROM subjects WHERE id=35481; SELECT SQL_CACHE * FROM posts WHERE id=35481 ORDER BY postdate ASC LIMIT 21 OFFSET 0; // Search 2 tables in 1 query SELECT SQL_CACHE subjects.lastmodified, subjects.subject, subjects.firstname, subjects.lastname, subjects.shortdesc, posts.postdate, posts.username, posts.comment FROM subjects, posts WHERE subjects.id=35481 AND posts.id=35481 ORDER BY postdate ASC LIMIT 21 OFFSET 0 I'm not sure how to test the timing on this. If I do 2 queries, do I add the query times together to find the total, and then compare that to the query time of the one that's 1 query to see which is faster? |
#7
| |||
| |||
|
|
This is for a message board, where I have 2 tables: "subjects", and "posts". Which is going to be faster and more efficient? You're fetching all of the subjects and all of the posts, but not relating each subject record a specific post record. This isn't really a join, so you're better off doing two queries. The second alternative creates the cross-product of the two selections, so if you have two subjects and five posts, you'll get back ten rather redundant result records. R's, John |
#8
| |||
| |||
|
|
This is for a message board, where I have 2 tables: "subjects", and "posts". No, he's fetching one subject and all the posts related to that one subject. He just hasn't learned how to join tables. |
#9
| |||
| |||
|
|
This is for a message board, where I have 2 tables: "subjects", and "posts". No, he's fetching one subject and all the posts related to that one subject. He just hasn't learned how to join tables. Depends what the id field is. Perhaps he could tell us. R's, John |
#10
| |||
| |||
|
|
On 12/6/2011 10:16 PM, John Levine wrote: This is for a message board, where I have 2 tables: "subjects", and "posts". No, he's fetching one subject and all the posts related to that one subject. He just hasn't learned how to join tables. Depends what the id field is. Perhaps he could tell us. R's, John From his query and questions, I would say it is the thread id. If it weren't, his message board wouldn't be working now (assuming, of course, this is the actual SQL he's using, which is pretty likely, IMHO). -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstu... (AT) attglobal (DOT) net ================== |
![]() |
| Thread Tools | |
| Display Modes | |
| |