0

I have a fairly static (InnoDB) table T with four columns: A, B, C and D.

I firstly wish to identify, for a given value of A, which value(s) of B yield unique C across all records. My attempt is as follows:

CREATE PROCEDURE P(x int) BEGIN
    SELECT   B
    FROM     T
    WHERE    A = x
    GROUP BY B
    HAVING   COUNT(DISTINCT C) = COUNT(C);
END

But introducing the GROUP BY dramatically reduces the performance of this query, despite there being an index on column B. Is there a more efficient way, or can I improve the peformance of this query somehow?


In response to Daan's comment below, the table was created with the following:

CREATE TABLE T (
    A int(11) NOT NULL,
    B varchar(45) NOT NULL,
    C varchar(255) DEFAULT NULL,
    D int(11) NOT NULL,
    PRIMARY KEY (A,B,D),
    KEY iA (A),
    KEY iB (B),
    KEY iC (C)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

In response to tombom's comment below, the query is explained as follows:

+----+-------------+-------+------+---------------+---------+---------+-------+---------+-----------------------------+
| id | select_type | table | type | possible_keys | key     | key_len | ref   | rows    | Extra                       |
+----+-------------+-------+------+---------------+---------+---------+-------+---------+-----------------------------+
| 1  | SIMPLE      | T     | ref  | PRIMARY,iA    | PRIMARY | 4       | const | 2603472 | Using where; Using filesort |
+----+-------------+-------+------+---------------+---------+---------+-------+---------+-----------------------------+
5
  • 1
    How many rows are there in this table? Can you show us a CREATE TABLE statement? Also, in MySQL, GROUP BY implies ORDER BY (dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html). Adding ORDER BY NULL might help? Don't think so in this case, though. Have you also tried SELECT DISTINCT B instead?
    – Daan
    Mar 27, 2012 at 9:30
  • @Daan: There are millions of rows (it's utterly huge); I will add the CREATE TABLE in a moment. ORDER BY NULL does not give any significant improvement. SELECT DISTINCT B (without a GROUP BY) prevents discovering which yield uniquely different C.
    – anon
    Mar 27, 2012 at 9:39
  • What does an explain say? Put the explain in front of your query and post the result
    – fancyPants
    Mar 27, 2012 at 10:05
  • 1
    @eggyal The PRIMARY KEY (in your edit) will naturally forbid any (A, B) pair from being repeated, therefore no two C values can ever be associated to the same (A, B). In other words, for any given A, each value of B yields unique C (because it yields a single C). This pretty much defeats the purpose of your question - what is your real table? Could you show us some test data? Mar 27, 2012 at 11:42
  • @BrankoDimitrijevic: You are absolutely right. For the sake of convenience I had indeed omitted that there is in fact another column that forms part of the PRIMARY KEY but is not of interest in this SELECT. I will edit the question to reflect the actual data.
    – anon
    Mar 27, 2012 at 11:53

2 Answers 2

2

You can try various approaches:

1.) Create an index over A,B and C like this

CREATE INDEX iABC ON T(A,B,C);

Since the problem is most likely the HAVING clause (C column as varchar(255) ain't that great in this case):

2.) Create a (temporary or not) table and then join to it. This might speed up things. A non-temporary like in the following might be faster, since you can create an index on it.

CREATE TABLE foo AS
SELECT 
B, 
COUNT(DISTINCT C) AS distinctC, 
COUNT(C) AS countC 
FROM T 
GROUP BY B;

CREATE INDEX idx_b ON foo(B);
CREATE INDEX idx_cc ON foo(distinctC, countC);

SELECT   T.B
FROM     T
INNER JOIN foo ON T.B = foo.B
WHERE    A = x
AND foo.distinctC = foo.countC
GROUP BY B
ORDER BY NULL; /*see Daan's comment*/

3.) Put the C column in a separate table, where the actual content is identified by an INT.

CREATE TABLE T (
    A int(11) NOT NULL,
    B varchar(45) NOT NULL,
    C int(11) DEFAULT NULL,
    PRIMARY KEY (A,B),
    KEY iB (B),
    KEY iC (C)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE C (
    id int(11) NOT NULL,
    Ccontent varchar(255) DEFAULT NULL
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Then do everything like usual and join later when you have your result to table C, to translate the ids with the actual varchar value.

I'd prefer option 2. And by the way, your index iA might be useless.

3
  • @Eggyal, in addition to what tombom described with the compound index on (a, b, c), if c's varchar is more of an available list of choices vs just any randomness, you might be MUCH BETTER using an enumerated list where the descriptions are long for readability, but store as integer which is very fast to work with... Just a thought.
    – DRapp
    Mar 27, 2012 at 11:51
  • @tombom: Thank you for your suggestions - I will experiment with each and report back.
    – anon
    Mar 27, 2012 at 11:59
  • @DRapp: Thanks for the suggestion, but sadly the list of possibilities is not enumerable.
    – anon
    Mar 27, 2012 at 11:59
0

Why not do COUNT(DISTINCT C)=1 instead?

1
  • Because I'm not looking for cases where all C are the same; I'm looking for cases where all C are uniquely different.
    – anon
    Mar 27, 2012 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.