1

I know that this title is overused, but it seems that my kind of question is not answered yet. So, the problem is like this:

I have a table structure made of four tables (tables, rows, cols, values) that I use to recreate the behavior of the information_schema (in a way). In php I am generating queries to retrieve the data, and the result would still look like a normal table:

SELECT 
  (SELECT value FROM `values` WHERE `col` = "3" and row = rows.id) as "col1",
  (SELECT value FROM `values` WHERE `col` = "4" and row = rows.id) as "col2" 
FROM rows WHERE `table` = (SELECT id FROM tables WHERE name = 'table1') 
HAVING (col2 LIKE "%4%")

OR

SELECT * FROM 
(SELECT 
  (SELECT value FROM `values` WHERE `col` = "3" and row = rows.id) as "col1",
  (SELECT value FROM `values` WHERE `col` = "4" and row = rows.id) as "col2" 
FROM rows WHERE `table` = (SELECT id FROM tables WHERE name = 'table1')) d 
WHERE col2 LIKE "%4%" 

note that the part where I define the columns of the result is generated by a php script. It is less important why I am doing this, but I want to extend this algorithm that generates the queries for a broader use. And we got to the core problem, I have to decide if I will generate a where or a having part for the query, and I know when to use them both, the problem is my algorithm doesn't and I have to make a few extra checks for this. But the two above queries are equivalent, I can always put any query in a sub-query, give it an alias, and use where on the new derived table. But I wonder if I will have problems with the performance or not, or if this will turn back on me in an unexpected way.

I know how they both work, and how where is supposed to be faster, but this is why I came here to ask. Hopefully I made myself understood, please excuse my english and the long useless turns of phrases, and all.

EDIT 1

I already know the difference between the two, and all that implies, my only dilemma is that using custom columns from other tables, with variable numbers and size, and trying to achieve the same result as using a normally created table implies that I must use HAVING for filtering the derived tables columns, at the same time having the option to wrap it up in a subquery and use where normally, this probably will create a temporary table that will be filtered afterwards. Will this affect performance for a large database? And unfortunately I cannot test this right now, as I do not afford to fill the database with over 1 billion entries (that will be something like this: 1 billion in rows table, 5 billions in values table, as every row have 5 columns, 5 rows in cols table and 1 row in tables table = 6,000,006 entries in total)

right now my database looks like this:

+----+--------+-----------+------+
| id | name   | title     | dets |
+----+--------+-----------+------+
|  1 | table1 | Table One |      |
+----+--------+-----------+------+

+----+-------+------+
| id | table | name |
+----+-------+------+
|  3 |     1 | col1 |
|  4 |     1 | col2 |
+----+-------+------+
where `table` is a foreign key from table `tables` 


+----+-------+-------+
| id | table | extra |
+----+-------+-------+
|  1 |     1 |       |
|  2 |     1 |       |
+----+-------+-------+
where `table` is a foreign key from table `tables` 

+----+-----+-----+----------+
| id | row | col | value    |
+----+-----+-----+----------+
|  1 |   1 |   3 | 13       |
|  2 |   1 |   4 | 14       |
|  6 |   2 |   4 | 24       |
|  9 |   2 |   3 | asdfghjk |
+----+-----+-----+----------+
where `row` is a foreign key from table `rows` 
where `col` is a foreign key from table `cols` 

EDIT 2

The conditions are there just for demonstration purposes!

EDIT 3

For only two rows, it seems there is a difference between the two, the one using having is 0,0008 and the one using where is 0.0014-0.0019. I wonder if this will affect performance for large numbers of rows and columns

EDIT 4

The result of the two queries is identical, and that is:

+----------+------+
| col1     | col2 |
+----------+------+
| 13       | 14   |
| asdfghjk | 24   |
+----------+------+
4

4 回答 4

4

HAVING是专门为GROUP BYWHERE是提供条件参数。另请参阅WHERE 与 HAVING

于 2013-07-30T14:10:24.340 回答
1

我相信在这种情况下,有子句会更快,因为您正在定义特定的值,而不是通读值并寻找匹配项。

于 2013-07-30T14:08:00.483 回答
1

请参阅:http ://database-programmer.blogspot.com/2008/04/group-by-have-sum-avg-and-count.html

基本上,WHERE在将列传递给聚合函数之前过滤掉它们,但HAVING过滤聚合函数的结果。

于 2013-07-30T14:11:57.037 回答
0

你可以那样做

    WHERE col2 In (14,24)

您的代码WHERE col2 LIKE "%4%"是个坏主意,那么 col2 = 34 也会被选中。

于 2013-07-30T14:17:22.707 回答