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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:16:18+00:00 2026-06-02T04:16:18+00:00

I have two tables in MySQL, apps and icons, each with about 750K rows.

  • 0

I have two tables in MySQL, “apps” and “icons”, each with about 750K rows. In Hibernate I was modeling them like:

public class App {
    @Basic
    private String title;

    @OneToOne(mappedBy = "app")
    private Icon icon;
    // etc...
}

public class Icon {
    @Basic
    private String name;

    @OneToOne
    private App app;
    // etc...
}

When I added this relation I quickly ran into a performance problem- reading in a single App was taking > 1 second. I examined the SQL that Hibernate was producing and found it was joining like this:

select
    apps.id as app_id,
    apps.title as app_title,
    icons.id as icon_id,
    icons.name as icon_name
from
    apps
left outer join
    icons
        on apps.id=icons.app_id 
where
    apps.id="zyz";

I found that adding @Fetch(FetchMode.SELECT) to the annotation greatly sped up the performance, bringing it down to around 30ms for effectively the same result. Here’s the produced SQL with the @Fetch(FetchMode.SELECT) annotation:

select
    apps.id as app_id,
    apps.title as title
from
    apps 
where
    apps.id="xyz";


select
    icons.id as icon_id,
    icons.name as icon_name
from
    icons
where
    icons.app_id="xyz";

Why is the left outer join so much slower? “Explain” on the joined query shows:

+----+-------------+-------+-------+---------------+---------+---------+-------+--------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows   | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+--------+-------+
|  1 | SIMPLE      | apps  | const | PRIMARY       | PRIMARY | 767     | const |      1 |       | 
|  1 | SIMPLE      | icons | ALL   | NULL          | NULL    | NULL    | NULL  | 783556 |       | 
+----+-------------+-------+-------+---------------+---------+---------+-------+--------+-------+

So it’s apparently visiting every row, vs a single row for the multiple-select query. Can’t the join use the index that I have on icons.app_id?

PS: yes, I used “RESET QUERY CACHE” in-between timing runs.

Update: moved to a bigint primary key, used that to join the tables in instead of the VARCHAR, and performance of the join is now on par with the “multiple selects” method.

  • 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-06-02T04:16:19+00:00Added an answer on June 2, 2026 at 4:16 am

    According to your explain, I believe the issue is with your schema at the database layer and not at the application layer.

    The fact that the join to the icons table has no entry for possible_keys leads me to believe you are running a MyISAM storage engine or an InnoDB storage engine with no FK constraints. Also, a key length of 767 strikes me as unusual, I’ve only ever seen this value < 10.

    • If the engine is MyISAM: Add an index to the icons.app_id column. And consider using an InnoDB engine so you can establish FK constraints so that you do not end up with orphaned rows.

    • If the engine is InnoDB: Add a FK constraint to the icons.app_id which references apps.id. By adding a FK constraint not only are you ensuring your data doesn’t become orphaned, you are also optimizing the joins between the tables because you are forced to create an index on both column.

    Either of the solutions mentioned above should greatly improve your performance. Let me know how it goes.

    You can read more info about some discussed topics using these links:

    • InnoDB Storage Engine

    • Foreign Key Constraints

    — Update —

    Here are some example alters when you are ready to add the INT columns, remember to do this on dev first and make sure this resolves the issue before pushing to production.

    For the apps table:

     ALTER TABLE apps
         ADD COLUMN idi int(11) UNSIGNED auto_increment FIRST,
         DROP PRIMARY KEY,
         ADD PRIMARY KEY(idi);
    

    For the icons table:

     ALTER TABLE icons
         ADD COLUMN app_idi int(11) UNSIGNED auto_increment AFTER app_id,
         ADD INDEX (app_idi ),
         ADD FOREIGN KEY (app_idi) REFERENCES apps(app_idi) ON DELETE CASCADE;
    

    These alters are demonstrative only, but should be enough to get you started in the right direction. You can read up on the MySQL docs I posted about Foreign Key Constraints for more information. Now, with the FK constraint setup between apps and icons, if any apps are deleted, and rows with icons matching app.id will also be deleted, ensuring you don’t orphan any data. If you don’t want to delete the associated rows in the icons table, you can change ON DELETE CASCADE to ON DELETE NULL, and they will be unlinked from the apps table, but still reside in the icons table.

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

Sidebar

Related Questions

I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have two MySQL tables (product and price history) that I would like to
I have two MySQL tables: collections and privacy_level . I define them with a
I have two tables in MySQL DB; table1, table2. Both of them have a
I have two MySQL tables and would like to select the highest value results
I have two MySQL tables named 'nodes' and 'joinTable' like shown below. I need
I have two tables in mysql: Results Table : 1046928 rows. Nodes Table :
I have two MySQL tables and I would like to know if there is
I have two MySql tables like the following: Cat ------ id : INT NOT
I have two tables in MySQL that are related. I would like to find

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.