sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
moltendorf's Avatar

Is this inefficient? (A little too much complexity.)


Question posted by: moltendorf (Newbie) on August 25th, 2008 07:08 PM
Alright, I finally worked up a massive SQL query that gets me the result I want for my PHP 6 CMS: Module Engine Class.

I have read various comments within the dev.mysql.com documentation of MySQL 5.0 on JOIN, IN, and certain subqueries being much faster if done in a different form.

So even though I'm fairly sure this wont be much of a burden if one uses my CMS with a fairly normal amount of modules installed (maybe a couple hundred or so tops) I'm more concerned about someone who bogs down their install with a ton of modules installed (maybe a couple thousand modules). I'm not so sure how efficient this query would be on a set of 3 tables...

Table: modules (note I removed 'module_' from the column names)
Expand|Select|Wrap|Line Numbers
  1. identifier replaces parent super_parent name                      is_active is_primary directory
  2. ------------------------------------------------------------------------------------------------
  3. 1          0        0      1            Primary                   1         0          primary
  4. 2          0        0      2            Test Module News          1         1          test
  5. 3          0        0      0            Session Management        1         1          session
  6. 4          0        0      4            Auto Module 1             1         0          am1
  7. 5          0        0      5            Auto Module 2             1         0          am2
  8. 6          0        0      6            Auto Module 3             1         0          am3
  9. 7          0        6      6            Auto Module 3_Sub         1         0          am3s
  10. 8          5        0      5            Auto Module 2 Replacement 1         0          am2r

Table: modules_auto (note I removed 'auto_' from the column names)
Expand|Select|Wrap|Line Numbers
  1. module_identifier module_active sort
  2. ------------------------------------
  3. 4                 3             1
  4. 5                 3             2
  5. 6                 3             3

Table: modules_streams (note I removed 'stream_' from the column names)
Expand|Select|Wrap|Line Numbers
  1. identifier module_identifier sort
  2. ---------------------------------
  3. 1          1                 0
  4. 2          2                 0
  5. 3          3                 0
  6. 4          4                 0
  7. 5          5                 0
  8. 6          6                 1
  9. 6          7                 0
  10. 7          7                 0

SQL Query:
Expand|Select|Wrap|Line Numbers
  1. SET @current_identifier = 
  2.     (
  3.         SELECT `module`.`module_identifier`
  4.             FROM `modules` `module`
  5.             WHERE `module`.`module_directory` = IF
  6.                 (
  7.                     (
  8.                         SELECT COUNT(`module`.`module_identifier`)
  9.                             AS `total`
  10.                             FROM `modules` `module`
  11.                             WHERE
  12.                                     `module`.`module_is_active` = '1'
  13.                                 AND `module`.`module_is_primary` = '1'
  14.                                 AND `module`.`module_directory` = 'session'
  15.                     ) > (0),
  16.                     'session',
  17.                     'test'
  18.                 )
  19.     );
  20. SELECT
  21.     `stream`.`stream_identifier`,
  22.     `stream`.`stream_sort`,
  23.     `module`.`module_identifier`,
  24.     `module`.`module_replaces`,
  25.     `module`.`module_name`,
  26.     `module`.`module_directory`,
  27.     `auto`.`auto_module_active`,
  28.     `auto`.`auto_sort`
  29.     FROM `modules_streams` `stream`
  30.         LEFT JOIN `modules` `module`
  31.             ON
  32.                 (
  33.                         `module`.`module_identifier` = `stream`.`stream_module_identifier`
  34.                     OR `module`.`module_replaces` = `stream`.`stream_module_identifier`
  35.                 )
  36.         LEFT OUTER JOIN `modules_auto` `auto`
  37.             ON
  38.                 (
  39.                         `module`.`module_identifier` = `auto`.`auto_module_identifier`
  40.                     OR `module`.`module_replaces` = `auto`.`auto_module_identifier`
  41.                 )
  42.     WHERE `stream`.`stream_identifier` IN
  43.         (
  44.             SELECT `module`.`module_identifier`
  45.                 FROM `modules` `module`
  46.                     LEFT OUTER JOIN `modules_auto` `auto`
  47.                         ON `module`.`module_identifier` = `auto`.`auto_module_identifier`
  48.                 WHERE
  49.                         `module`.`module_identifier` = '1'
  50.                     OR `auto`.`auto_module_active` = '1'
  51.                     OR `module`.`module_identifier` = @current_identifier
  52.                     OR `auto`.`auto_module_active` = @current_identifier
  53.         )
  54.     ORDER BY
  55.         `stream`.`stream_identifier` ASC,
  56.         `stream`.`stream_sort` ASC;

Result Set:
Expand|Select|Wrap|Line Numbers
  1. stream_identifier stream_sort module_identifier module_replaces module_name               module_directory auto_module_active auto_sort
  2. ---------------------------------------------------------------------------------------------------------------------------------------
  3. 1                 0           1                 0               Primary                   primary          NULL               NULL
  4. 3                 0           3                 0               Session Management        session          NULL               NULL
  5. 4                 0           4                 0               Auto Module 1             am1              3                  1
  6. 5                 0           5                 0               Auto Module 2             am2              3                  2
  7. 5                 0           8                 5               Auto Module 2 Replacement am2r             3                  2
  8. 6                 0           7                 0               Auto Module 3_Sub         am3s             NULL               NULL
  9. 6                 1           6                 0               Auto Module 3             am3              3                  3
0 Answers Posted
Reply
Not the answer you were looking for? Post your question . . .
197,031 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,031 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top MySQL Contributors