473,411 Members | 2,285 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,411 software developers and data experts.

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

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
Aug 25 '08 #1
0 1209

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

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
4
by: Stefan Berglund | last post by:
I've noticed that most examples offered in this group tend to use Response.Write to spit out the HTML rather than switch between script and HTML. I also came across an article intimating the same...
4
by: Generic Usenet Account | last post by:
Consider two entities A and B such that there is a 1:n association between them. I mean that associated with each instance of A there are up to n instances of B. Currently in our software we are...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
13
by: TS | last post by:
Say i have a class car with properties: Color, Make, Model, Year, DriverID And a Driver class with properties: DriverID, Name The driverID PRIVATE property is the id of the driver from say a...
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
9
by: beginner | last post by:
Hi All, I have a list of records like below: rec= Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is:
5
by: jeddiki | last post by:
Hello, I have been writing procedural php for a couple of years and I have quite lot of little scrips that I use in different large scripts. I'll give and example I display the category...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.