Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:03:26+00:00 2026-05-31T22:03:26+00:00

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

  • 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 |
+----+-------------+-------+------+---------------+---------+---------+-------+---------+-----------------------------+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:03:27+00:00Added an answer on May 31, 2026 at 10:03 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have fairly large C++ application which is composed of about 60 projects in
I have a fairly simple data audit web application written with ASP MVC which
I'm currently in the process of developing a fairly large static library which will
Suppose you have a private static method called Inst() which allows the class to
Question I have a fairly static website with just a few basic PHP usage.
I currently have a fairly long LINQ statement which pulls data from two tables:
this is my first posted question. I have a fairly complicated OQL query which
I have a fairly trivial static variable question. I'm building out a solution that
I've written a migration to populate a countries table with fairly static data. It
I have a fairly busy site which does around 10m views a month. One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.