简体   繁体   中英

Multiple SQL queries on one table

I am using LAMP. In MySql I have a table with columns X, Y and Z. I need to select distinct records from multiple sets and subsets. If my initial criteria is WHERE Y = A; then I need to be able to:

  • Get set B by selecting records where Y = A,
  • Get set C by selecting records where X is in set B and Z = 123
  • Get set D by selecting records where Y is in set B and Z = 456
  • Get set E by selecting records where X is in set D and Z = 789

The table could get quite large and I'm not really sure where to start with this. Some possible approaches would be to:

  1. Have multiple individual queries. In this case, how would I cache the result set from one query for use as input to the other? Memcached?
  2. Use nested subqueries. But the results of the inner query are purely for restricting the the outer query, aren't they? How would I get them to appear in the result?
  3. Use UNION. But how would I store the result of one query to use as input to another?
  4. JOIN the table to itself multiple times. This would produce a cartesian result where the outer rows are repeated multiple times. This would map to a multi-dimensional PHP array which ought to be manageable. What about performance though?

I am leaning toward option number 4, but not 100% sure. Am I reinventing the wheel?

Much appreciated...

I do not understand your goal because you haven't defined a restriction on X, however from your description I believe that set E would be empty. I say this because set D already contains rows where Z is 456. So when you run another query on that resultset to generate E for the condition where Z is 789, you will not get any rows back because all the rows in D are 456.

Now to personal opinions:

  1. Avoid Cartesian products whenever possible - Joins are good but be careful, avoid cross products if possible
  2. If needed denormalize tables to improve query efficiency
  3. Generate queries that produce the smallest resultset possible to meet your requirements
  4. If possible use sub-queries to reduce resultset size
  5. Trust the query optimizer so avoid subqueries if possible
  6. Make SQL do the work whenever possible as it has the ability and resources to calculate the data in the most optimal manner

In order to highlight item #4 above, I would like to mention that in your example for D, you can write the following query without subqueries or any other temporary storage:

SELECT * FROM MyTable WHERE Y = A AND Z = 456

Now, maybe I can attempt to solve your dilemma by guessing what you want:

SELECT * FROM MyTable WHERE Y = A AND Z IN (123, 456, 789)

Or:

SELECT * FROM MyTable WHERE Y = A AND (Z = 123 OR Z = 456 OR Z = 789)

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM