![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm hoping some SQL gurus read this and help me optimize my table/ query. ![]() The table is shown below: CREATE TABLE IF NOT EXISTS PACKAGE_TABLE ( ID BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, TAG_ID BIGINT UNSIGNED NOT NULL, LAST_UPDATE TIMESTAMP, JOB_NAME MEDIUMTEXT, STATE TEXT, ERROR_CODES INT UNSIGNED, OPERATOR MEDIUMTEXT, TAG_FIRMWARE TINYTEXT, . . . PLUS 5 OTHER TEXT FIELDS NOT IMPORTANT FOR THIS DICSUSSION ) In the table I will be storing multiple records keyed by the same TAG_ID. Essientlly every iteraction of each tag is stored and thus I can recover a tag's history. For an important report I want to query for the last update for each TAG_ID. I have come up with the following: SELECT * FROM PACKAGE_TABLE t1 WHERE JOB_NAME = 'Some Job' AND LAST_UPDATE = (SELECT MAX(t2.LAST_UPDATE) FROM PACKAGE_TABLE t2 where t1.TAG_ID = t2.TAG_ID) This query works but on large tables (10000+ tag ids) the query really slows. Does anyone know of a more efficient way to format the table/ query for increased speed. |
#3
| |||
| |||
|
|
I'm hoping some SQL gurus read this and help me optimize my table/ query. ![]() The table is shown below: CREATE TABLE IF NOT EXISTS PACKAGE_TABLE ( ID BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, TAG_ID BIGINT UNSIGNED NOT NULL, LAST_UPDATE TIMESTAMP, JOB_NAME MEDIUMTEXT, STATE TEXT, ERROR_CODES INT UNSIGNED, OPERATOR MEDIUMTEXT, TAG_FIRMWARE TINYTEXT, . . . PLUS 5 OTHER TEXT FIELDS NOT IMPORTANT FOR THIS DICSUSSION ) In the table I will be storing multiple records keyed by the same TAG_ID. Essientlly every iteraction of each tag is stored and thus I can recover a tag's history. For an important report I want to query for the last update for each TAG_ID. I have come up with the following: SELECT * FROM PACKAGE_TABLE t1 WHERE JOB_NAME = 'Some Job' AND LAST_UPDATE = (SELECT MAX(t2.LAST_UPDATE) FROM PACKAGE_TABLE t2 where t1.TAG_ID = t2.TAG_ID) This query works but on large tables (10000+ tag ids) the query really slows. Does anyone know of a more efficient way to format the table/ query for increased speed. |
#4
| |||
| |||
|
|
On 13 Nov, 01:52, Dean.Brot... (AT) gmail (DOT) com wrote: I'm hoping some SQL gurus read this and help me optimize my table/ query. ![]() The table is shown below: CREATE TABLE IF NOT EXISTS PACKAGE_TABLE ( ID BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, TAG_ID BIGINT UNSIGNED NOT NULL, LAST_UPDATE TIMESTAMP, JOB_NAME MEDIUMTEXT, STATE TEXT, ERROR_CODES INT UNSIGNED, OPERATOR MEDIUMTEXT, TAG_FIRMWARE TINYTEXT, . . . PLUS 5 OTHER TEXT FIELDS NOT IMPORTANT FOR THIS DICSUSSION ) In the table I will be storing multiple records keyed by the same TAG_ID. Essientlly every iteraction of each tag is stored and thus I can recover a tag's history. For an important report I want to query for the last update for each TAG_ID. I have come up with the following: SELECT * FROM PACKAGE_TABLE t1 WHERE JOB_NAME = 'Some Job' AND LAST_UPDATE = (SELECT MAX(t2.LAST_UPDATE) FROM PACKAGE_TABLE t2 where t1.TAG_ID = t2.TAG_ID) This query works but on large tables (10000+ tag ids) the query really slows. Does anyone know of a more efficient way to format the table/ query for increased speed. You need the strawberry query which is mentioned many times in this group. This avoids the use of a subquery completely and is the most eficient way of doing this sort of query. Look at the last query on this page (before the user comments): http://dev.mysql.com/doc/refman/5.0/...group-row.html |
#5
| |||
| |||
|
|
"Captain Paralytic" <paul_laut... (AT) yahoo (DOT) com> wrote in message news:1194947263.585654.202740 (AT) v65g2000hsc (DOT) googlegroups.com... On 13 Nov, 01:52, Dean.Brot... (AT) gmail (DOT) com wrote: I'm hoping some SQL gurus read this and help me optimize my table/ query. ![]() The table is shown below: CREATE TABLE IF NOT EXISTS PACKAGE_TABLE ( ID BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, TAG_ID BIGINT UNSIGNED NOT NULL, LAST_UPDATE TIMESTAMP, JOB_NAME MEDIUMTEXT, STATE TEXT, ERROR_CODES INT UNSIGNED, OPERATOR MEDIUMTEXT, TAG_FIRMWARE TINYTEXT, . . . PLUS 5 OTHER TEXT FIELDS NOT IMPORTANT FOR THIS DICSUSSION ) In the table I will be storing multiple records keyed by the same TAG_ID. Essientlly every iteraction of each tag is stored and thus I can recover a tag's history. For an important report I want to query for the last update for each TAG_ID. I have come up with the following: SELECT * FROM PACKAGE_TABLE t1 WHERE JOB_NAME = 'Some Job' AND LAST_UPDATE = (SELECT MAX(t2.LAST_UPDATE) FROM PACKAGE_TABLE t2 where t1.TAG_ID = t2.TAG_ID) This query works but on large tables (10000+ tag ids) the query really slows. Does anyone know of a more efficient way to format the table/ query for increased speed. You need the strawberry query which is mentioned many times in this group. This avoids the use of a subquery completely and is the most eficient way of doing this sort of query. Look at the last query on this page (before the user comments): http://dev.mysql.com/doc/refman/5.0/...column-group-r... I have to take the web page's word for it when it says that's an efficient solution for MySQL, but that is the kind of optimization that I'd expect a reasonable 21st century DBMS to find all by itself based on the original query, which was a perfectly correct specification of the required result. Roy- Hide quoted text - - Show quoted text - |
#6
| ||||
| ||||
|
|
On 13 Nov, 10:21, "Roy Hann" <specia... (AT) processed (DOT) almost.meat> wrote: I have to take the web page's word for it when it says that's an efficient solution for MySQL, but that is the kind of optimization that I'd expect a reasonable 21st century DBMS to find all by itself based on the original query, which was a perfectly correct specification of the required result. Then you're expecting too much. |
|
SQL programmers are assumed to possess a certain level of skill I guess. |
|
The structures of the query are wildly different. |
|
How is an optimizer to know whether you have a specific reason for constructing a query in a particular way. |
#7
| |||
| |||
|
|
I also expect that most working programmers have little or no interest in SQL, regard it as a distraction, |
#8
| |||
| |||
|
|
Now who's expecting too much? I expect DBMSs to be designed and developed by the very best and brightest developers. I think that is a very reasonable expectation. I also expect that most working programmers have little or no interest in SQL, regard it as a distraction, and would very much prefer the machine to do the best job possible with a correct description of the required result. |
#9
| |||
| |||
|
|
On 13 Nov, 12:07, "Roy Hann" <specia... (AT) processed (DOT) almost.meat> wrote: I also expect that most working programmers have little or no interest in SQL, regard it as a distraction, That's why there are SQL programmers! |
#10
| |||
| |||
|
|
Now who's expecting too much? I expect DBMSs to be designed and developed by the very best and brightest developers. I think that is a very reasonable expectation. I also expect that most working programmers have little or no interest in SQL, regard it as a distraction, and would very much prefer the machine to do the best job possible with a correct description of the required result. That a program is written by the brightest developers does not guarantee that willfully ignorant people can generate optimized results. |
|
By the way, you expect wrong. I have a responsibility to use the database as a part of the systems I build. Database traffic is not a distraction. It can ruin my entire system if it does not work as it should. |
|
I, on the other hand, expect a programmer who does not give a dime about his system to stay quiet about optimizations. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |