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 6326191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:02:36+00:00 2026-05-24T17:02:36+00:00

I hava a table items and a table item_attributes . For simplicity let’s say

  • 0

I hava a table items and a table item_attributes.

For simplicity let’s say my table item has a column id and a column name.
Of cource there is a index on the id column.

the item_attributes table has the columns id, item_id, attribute_name and attribute_value and an index ON attrubute_name

Now I want to query all items with a specific attribut without using a join.

I do this with the following query:

SELECT *
FROM items i
WHERE i.id IN (
    SELECT item_id
    FROM item_attributes a
    WHERE a.attribute_name = 'SomeAttribute'
      AND a.attribute_value = 'SomeValue'
)

the SubQuery itself runs fast.

If I execute the query itself first and use the result for an IN query

SELECT *
FROM items i
WHERE i.id IN (1,3,5,7,10,...)

it is fast, too.

However, combined the query is very, very slow (>2 secs.)
If I investigate the query plan I see why: MySQL does a full table scan on the items table instead of executing the subquery first and using the result for an index query.

1, 'PRIMARY', 'items', 'ALL', '', '', '', '', 149726, 'Using where'
2, 'DEPENDENT SUBQUERY', 'item_attributes', 'index_subquery', 'IDX_ATTRIBUTE_NAME', 'IDX_ATTRIBUTE_NAME', '4', 'func', 1, 'Using where'

Is there a way to optimize this query? I know that the subquery will always return only a small resultset (<100 rows).

  • 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-24T17:02:36+00:00Added an answer on May 24, 2026 at 5:02 pm

    MySQL cannot switch the leading and the driven table in the IN clause. This is going to be corrected in 6.0.

    For now, you can rewrite it like this (requires a JOIN):

    SELECT  i.*
    FROM    (
            SELECT  DISTINCT item_id
            FROM    item_attributes a
            WHERE   a.attribute_name = 'SomeAttribute'
                    AND a.attribute_value = 'SomeValue'
            ) ai
    JOIN    items i
    ON      i.id = ai.item_id
    

    Since you are using the EAV model you may want to make a unique index on (attribute_name, item_id) in which case you can use a plain join:

    SELECT  i.*
    FROM    item_attributes ai
    JOIN    items i
    ON      i.id = ai.item_id
    WHERE   a.attribute_value = 'SomeValue'
            AND a.attribute_name = 'SomeAttribute'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava an editor template for, let's say, date: @model DateTime @section AdditionalJavaScript2 {
I hava a table in a scrollPane . There are 6 columns in my
I hava the sql as below: select a.dept, a.name from students a group by
I hava very large table with 1373228 rows. I need to copy the result
i hava a placed a select statement inside a table cell. The ui was
Is there any methode to return a data table as a particular cell of
I hava a simple set of two related tables of an 'order' that has
I hava a url like mysite.net/home/index/page/XX while XX is any number. I need to
I hava a RadioButtonList control on my page that has multiple option.I want to
I hava data in an Oracle table that is organized as a graph that

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.