[Solved] Select a column from table based on other column values
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Select a column from table based on other column values

Database type:

I have a table in MySql and table name is logs

+---------------+---------------+------+-----+---------+-------+
| Field         | Type          | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| domain        | varchar(50)   | YES  | MUL | NULL    |       |
| sid           | varchar(100)  | YES  | MUL | NULL    |       |
| email         | varchar(100)  | YES  | MUL | NULL    |       |
+---------------+---------------+------+-----+---------+-------+

The following are sample rows from the table

+------------+----------------+---------------
| sid        | email                 | domain|
+------------------------------------+-------+
| 1 |        | [email protected]      | xxx   |
| 2 |        | [email protected]      | xxx   |
| 2 |        | [email protected]      | yyy   |
| 2 |        | [email protected]      | yyy   |
| 3 |        | [email protected]      | zzz   |
| 4 |        | [email protected]      | qqq   |
| 2 |        | [email protected]      | ppp   |
+---+--------+-----------------------+-------+

I want a query something like

select * from logs
where sid IN (select sid from logs
              where domain="xxx" AND email="[email protected]")

Desired output

+------------+-----------------------+--------
| sid        | email                 | domain|
+------------------------------------+-------+
| 1 |        | [email protected]      | xxx   |
| 2 |        | [email protected]oo.com      | xxx   |
| 2 |        | [email protected]      | yyy   |
| 2 |        | [email protected]      | yyy   |
| 2 |        | [email protected]      | ppp   |
+---+--------+-----------------------+-------+

I can do it using joins but is there any way to get results without using joins or any optimized version of this query

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `logs` ADD INDEX `logs_idx_sid_domain_email_com` (`sid`,`domain`,`email`,`com`);
The optimized query:
SELECT
        * 
    FROM
        logs 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                logs AS logs1 
            WHERE
                (
                    logs1.domain = 'xxx' 
                    AND logs1.email = logs1."[email protected]"
                ) 
                AND (
                    logs.sid = logs1.sid
                )
        )

Related Articles



* original question posted on StackOverflow here.