MySQL poor select query performance despite indexes2019 Community Moderator ElectionHow to output MySQL query results in CSV format?MySQL - UPDATE query based on SELECT QueryHow do I add indices to MySQL tables?Add Foreign Key to existing tablecreating mysql indexOptimizing MySQL Count query with multiple joinsMySQL Refusing to Use Index for Simple QueryMYSQL Query Performance with Distinct, order by and limit against same tablemysql performs full table scan even though index existsPoor query performance with index compared to without index

Sort array by month and year

Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?

Why does a car's steering wheel get lighter with increasing speed

What is the oldest European royal house?

Mixed Feelings - What am I

Is it appropriate to ask a former professor to order a library book for me through ILL?

Unfamiliar notation in Diabelli's "Duet in D" for piano

Is the differential, dp, exact or not?

Why aren't there more Gauls like Obelix?

Short SF story. Females use stingers to implant eggs in yearfathers

Are small insurances worth it?

Do I need a return ticket to Canada if I'm a Japanese National?

How to distinguish easily different soldier of ww2?

Rationale to prefer local variables over instance variables?

Boss Telling direct supervisor I snitched

How would an energy-based "projectile" blow up a spaceship?

How does learning spells work when leveling a multiclass character?

What is the purpose of a disclaimer like "this is not legal advice"?

Use Mercury as quenching liquid for swords?

Vector-transposing function

Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?

Is it a Cyclops number? "Nobody" knows!

What would be the most expensive material to an intergalactic society?

Averaging over columns while ignoring zero entries



MySQL poor select query performance despite indexes



2019 Community Moderator ElectionHow to output MySQL query results in CSV format?MySQL - UPDATE query based on SELECT QueryHow do I add indices to MySQL tables?Add Foreign Key to existing tablecreating mysql indexOptimizing MySQL Count query with multiple joinsMySQL Refusing to Use Index for Simple QueryMYSQL Query Performance with Distinct, order by and limit against same tablemysql performs full table scan even though index existsPoor query performance with index compared to without index










0















We have two tables, messages and customercontracts defined as follows:



create table customercontracts (customer_id varchar(20), 
contractnumber varchar(20),
role varchar(4));
alter table customercontracts add index contractnumber (contractnumber);

create table messages (customer_id varchar(20),
contractnumber varchar(20),
message varchar(400));
alter table messages add index contractnumber (contractnumber);
alter table messages add index customer_id (customer_id );


And a query like this:



select * from messages m, customercontracts c 
where m.customer_id = '12345'
and c.contractnumber = m.contractnumber;


There are approximately 4,000 messages rows and 3,000,000 customercontracts rows. The above query takes approximately 4 seconds to execute despite there being indexes on both customer_id and contractnumber. 'Explain' (in MySQL Workbench) shows a full table scan on customercontracts and a query cost of 628,000.



Questions:



1) Why is there a full table scan on customercontracts when I have indexes on these tables? What is the cause of the poor performance?



2) How can I rewrite this query to be performant?










share|improve this question






















  • can you pls verify MySQL take the index if you exec:**SELECT * from customercontracts c WHERE c.contractnumber = VALIDNUMBER**

    – Bernd Buffen
    2 days ago











  • The PRIMARY KEY is unclear. Please clarify.

    – Strawberry
    2 days ago











  • @Strawberry - Apologies, the table definitions are simplified. The primary key is another column (not included). contract number and customer_id are separate non-unique keys (using BTREE). The data is highly unique on both tables.

    – Chris Knight
    2 days ago











  • @BerndBuffen - Yes, index is used for that statement

    – Chris Knight
    2 days ago















0















We have two tables, messages and customercontracts defined as follows:



create table customercontracts (customer_id varchar(20), 
contractnumber varchar(20),
role varchar(4));
alter table customercontracts add index contractnumber (contractnumber);

create table messages (customer_id varchar(20),
contractnumber varchar(20),
message varchar(400));
alter table messages add index contractnumber (contractnumber);
alter table messages add index customer_id (customer_id );


And a query like this:



select * from messages m, customercontracts c 
where m.customer_id = '12345'
and c.contractnumber = m.contractnumber;


There are approximately 4,000 messages rows and 3,000,000 customercontracts rows. The above query takes approximately 4 seconds to execute despite there being indexes on both customer_id and contractnumber. 'Explain' (in MySQL Workbench) shows a full table scan on customercontracts and a query cost of 628,000.



Questions:



1) Why is there a full table scan on customercontracts when I have indexes on these tables? What is the cause of the poor performance?



2) How can I rewrite this query to be performant?










share|improve this question






















  • can you pls verify MySQL take the index if you exec:**SELECT * from customercontracts c WHERE c.contractnumber = VALIDNUMBER**

    – Bernd Buffen
    2 days ago











  • The PRIMARY KEY is unclear. Please clarify.

    – Strawberry
    2 days ago











  • @Strawberry - Apologies, the table definitions are simplified. The primary key is another column (not included). contract number and customer_id are separate non-unique keys (using BTREE). The data is highly unique on both tables.

    – Chris Knight
    2 days ago











  • @BerndBuffen - Yes, index is used for that statement

    – Chris Knight
    2 days ago













0












0








0


1






We have two tables, messages and customercontracts defined as follows:



create table customercontracts (customer_id varchar(20), 
contractnumber varchar(20),
role varchar(4));
alter table customercontracts add index contractnumber (contractnumber);

create table messages (customer_id varchar(20),
contractnumber varchar(20),
message varchar(400));
alter table messages add index contractnumber (contractnumber);
alter table messages add index customer_id (customer_id );


And a query like this:



select * from messages m, customercontracts c 
where m.customer_id = '12345'
and c.contractnumber = m.contractnumber;


There are approximately 4,000 messages rows and 3,000,000 customercontracts rows. The above query takes approximately 4 seconds to execute despite there being indexes on both customer_id and contractnumber. 'Explain' (in MySQL Workbench) shows a full table scan on customercontracts and a query cost of 628,000.



Questions:



1) Why is there a full table scan on customercontracts when I have indexes on these tables? What is the cause of the poor performance?



2) How can I rewrite this query to be performant?










share|improve this question














We have two tables, messages and customercontracts defined as follows:



create table customercontracts (customer_id varchar(20), 
contractnumber varchar(20),
role varchar(4));
alter table customercontracts add index contractnumber (contractnumber);

create table messages (customer_id varchar(20),
contractnumber varchar(20),
message varchar(400));
alter table messages add index contractnumber (contractnumber);
alter table messages add index customer_id (customer_id );


And a query like this:



select * from messages m, customercontracts c 
where m.customer_id = '12345'
and c.contractnumber = m.contractnumber;


There are approximately 4,000 messages rows and 3,000,000 customercontracts rows. The above query takes approximately 4 seconds to execute despite there being indexes on both customer_id and contractnumber. 'Explain' (in MySQL Workbench) shows a full table scan on customercontracts and a query cost of 628,000.



Questions:



1) Why is there a full table scan on customercontracts when I have indexes on these tables? What is the cause of the poor performance?



2) How can I rewrite this query to be performant?







mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Chris KnightChris Knight

154




154












  • can you pls verify MySQL take the index if you exec:**SELECT * from customercontracts c WHERE c.contractnumber = VALIDNUMBER**

    – Bernd Buffen
    2 days ago











  • The PRIMARY KEY is unclear. Please clarify.

    – Strawberry
    2 days ago











  • @Strawberry - Apologies, the table definitions are simplified. The primary key is another column (not included). contract number and customer_id are separate non-unique keys (using BTREE). The data is highly unique on both tables.

    – Chris Knight
    2 days ago











  • @BerndBuffen - Yes, index is used for that statement

    – Chris Knight
    2 days ago

















  • can you pls verify MySQL take the index if you exec:**SELECT * from customercontracts c WHERE c.contractnumber = VALIDNUMBER**

    – Bernd Buffen
    2 days ago











  • The PRIMARY KEY is unclear. Please clarify.

    – Strawberry
    2 days ago











  • @Strawberry - Apologies, the table definitions are simplified. The primary key is another column (not included). contract number and customer_id are separate non-unique keys (using BTREE). The data is highly unique on both tables.

    – Chris Knight
    2 days ago











  • @BerndBuffen - Yes, index is used for that statement

    – Chris Knight
    2 days ago
















can you pls verify MySQL take the index if you exec:**SELECT * from customercontracts c WHERE c.contractnumber = VALIDNUMBER**

– Bernd Buffen
2 days ago





can you pls verify MySQL take the index if you exec:**SELECT * from customercontracts c WHERE c.contractnumber = VALIDNUMBER**

– Bernd Buffen
2 days ago













The PRIMARY KEY is unclear. Please clarify.

– Strawberry
2 days ago





The PRIMARY KEY is unclear. Please clarify.

– Strawberry
2 days ago













@Strawberry - Apologies, the table definitions are simplified. The primary key is another column (not included). contract number and customer_id are separate non-unique keys (using BTREE). The data is highly unique on both tables.

– Chris Knight
2 days ago





@Strawberry - Apologies, the table definitions are simplified. The primary key is another column (not included). contract number and customer_id are separate non-unique keys (using BTREE). The data is highly unique on both tables.

– Chris Knight
2 days ago













@BerndBuffen - Yes, index is used for that statement

– Chris Knight
2 days ago





@BerndBuffen - Yes, index is used for that statement

– Chris Knight
2 days ago












4 Answers
4






active

oldest

votes


















0














IS NOT THE ANSWER:



I have test it with ca. 1.000.000 rows in each table and they use the Index



MariaDB []> EXPLAIN SELECT * FROM messages m, customercontracts c
-> WHERE m.customer_id = 'CUST_22345'
-> AND c.contractnumber = m.contractnumber;
+------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
| 1 | SIMPLE | m | ref | contractnumber,customer_id | customer_id | 63 | const | 1 | Using index condition; Using where |
| 1 | SIMPLE | c | ref | contractnumber | contractnumber | 63 | test2.m.contractnumber | 1 | |
+------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
2 rows in set (0.002 sec)

MariaDB []> SELECT * FROM messages m, customercontracts c
-> WHERE m.customer_id = 'CUST_22345'
-> AND c.contractnumber = m.contractnumber;
+-------------+----------------+---------+-------------+----------------+------+
| customer_id | contractnumber | message | customer_id | contractnumber | role |
+-------------+----------------+---------+-------------+----------------+------+
| CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
| CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
| CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
| CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
+-------------+----------------+---------+-------------+----------------+------+
4 rows in set (0.009 sec)

MariaDB []>





share|improve this answer






























    0














    I would suggest try composite index on message Table customer_id,contractnumber and check performance, try to avoid using * in select .






    share|improve this answer






























      0














      I think I've finally found the answer to this. My sample table creation code above did not show the character encoding (a learn for me, don't shortcut sample code!). As it turns out, the customercontacts table is encoded in utf8 while the messages table is encoded in utf8mb4.



      When columns of different character encoding are joined, indexes cannot be used.



      This answers the 'why is this happening'. To fix this, we modified the utf8m4 table to downgrade the encoding of the indexed column to utf8 (while keeping the encoding of the rest of the table as utf8mb4). This has fixed performance for us.






      share|improve this answer
































        -1














        I'd consider adding an INTEGER "id" field as the primary key for customercontracts and then reference through customercontracts_id instead of contractnumber in the messages table. You should see an improvement in performance already.



        Alternatively, you can try adding FULLTEXT indexes on both contractnumber columns, but I'd advise for the first option.






        share|improve this answer








        New contributor




        T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023481%2fmysql-poor-select-query-performance-despite-indexes%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          IS NOT THE ANSWER:



          I have test it with ca. 1.000.000 rows in each table and they use the Index



          MariaDB []> EXPLAIN SELECT * FROM messages m, customercontracts c
          -> WHERE m.customer_id = 'CUST_22345'
          -> AND c.contractnumber = m.contractnumber;
          +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
          | 1 | SIMPLE | m | ref | contractnumber,customer_id | customer_id | 63 | const | 1 | Using index condition; Using where |
          | 1 | SIMPLE | c | ref | contractnumber | contractnumber | 63 | test2.m.contractnumber | 1 | |
          +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
          2 rows in set (0.002 sec)

          MariaDB []> SELECT * FROM messages m, customercontracts c
          -> WHERE m.customer_id = 'CUST_22345'
          -> AND c.contractnumber = m.contractnumber;
          +-------------+----------------+---------+-------------+----------------+------+
          | customer_id | contractnumber | message | customer_id | contractnumber | role |
          +-------------+----------------+---------+-------------+----------------+------+
          | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
          | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
          | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
          | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
          +-------------+----------------+---------+-------------+----------------+------+
          4 rows in set (0.009 sec)

          MariaDB []>





          share|improve this answer



























            0














            IS NOT THE ANSWER:



            I have test it with ca. 1.000.000 rows in each table and they use the Index



            MariaDB []> EXPLAIN SELECT * FROM messages m, customercontracts c
            -> WHERE m.customer_id = 'CUST_22345'
            -> AND c.contractnumber = m.contractnumber;
            +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
            | 1 | SIMPLE | m | ref | contractnumber,customer_id | customer_id | 63 | const | 1 | Using index condition; Using where |
            | 1 | SIMPLE | c | ref | contractnumber | contractnumber | 63 | test2.m.contractnumber | 1 | |
            +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
            2 rows in set (0.002 sec)

            MariaDB []> SELECT * FROM messages m, customercontracts c
            -> WHERE m.customer_id = 'CUST_22345'
            -> AND c.contractnumber = m.contractnumber;
            +-------------+----------------+---------+-------------+----------------+------+
            | customer_id | contractnumber | message | customer_id | contractnumber | role |
            +-------------+----------------+---------+-------------+----------------+------+
            | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
            | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
            | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
            | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
            +-------------+----------------+---------+-------------+----------------+------+
            4 rows in set (0.009 sec)

            MariaDB []>





            share|improve this answer

























              0












              0








              0







              IS NOT THE ANSWER:



              I have test it with ca. 1.000.000 rows in each table and they use the Index



              MariaDB []> EXPLAIN SELECT * FROM messages m, customercontracts c
              -> WHERE m.customer_id = 'CUST_22345'
              -> AND c.contractnumber = m.contractnumber;
              +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
              | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
              +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
              | 1 | SIMPLE | m | ref | contractnumber,customer_id | customer_id | 63 | const | 1 | Using index condition; Using where |
              | 1 | SIMPLE | c | ref | contractnumber | contractnumber | 63 | test2.m.contractnumber | 1 | |
              +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
              2 rows in set (0.002 sec)

              MariaDB []> SELECT * FROM messages m, customercontracts c
              -> WHERE m.customer_id = 'CUST_22345'
              -> AND c.contractnumber = m.contractnumber;
              +-------------+----------------+---------+-------------+----------------+------+
              | customer_id | contractnumber | message | customer_id | contractnumber | role |
              +-------------+----------------+---------+-------------+----------------+------+
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              +-------------+----------------+---------+-------------+----------------+------+
              4 rows in set (0.009 sec)

              MariaDB []>





              share|improve this answer













              IS NOT THE ANSWER:



              I have test it with ca. 1.000.000 rows in each table and they use the Index



              MariaDB []> EXPLAIN SELECT * FROM messages m, customercontracts c
              -> WHERE m.customer_id = 'CUST_22345'
              -> AND c.contractnumber = m.contractnumber;
              +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
              | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
              +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
              | 1 | SIMPLE | m | ref | contractnumber,customer_id | customer_id | 63 | const | 1 | Using index condition; Using where |
              | 1 | SIMPLE | c | ref | contractnumber | contractnumber | 63 | test2.m.contractnumber | 1 | |
              +------+-------------+-------+------+----------------------------+----------------+---------+------------------------+------+------------------------------------+
              2 rows in set (0.002 sec)

              MariaDB []> SELECT * FROM messages m, customercontracts c
              -> WHERE m.customer_id = 'CUST_22345'
              -> AND c.contractnumber = m.contractnumber;
              +-------------+----------------+---------+-------------+----------------+------+
              | customer_id | contractnumber | message | customer_id | contractnumber | role |
              +-------------+----------------+---------+-------------+----------------+------+
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              | CUST_22345 | 22345 | NULL | CUST_22345 | 22345 | NULL |
              +-------------+----------------+---------+-------------+----------------+------+
              4 rows in set (0.009 sec)

              MariaDB []>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 days ago









              Bernd BuffenBernd Buffen

              10.1k21024




              10.1k21024























                  0














                  I would suggest try composite index on message Table customer_id,contractnumber and check performance, try to avoid using * in select .






                  share|improve this answer



























                    0














                    I would suggest try composite index on message Table customer_id,contractnumber and check performance, try to avoid using * in select .






                    share|improve this answer

























                      0












                      0








                      0







                      I would suggest try composite index on message Table customer_id,contractnumber and check performance, try to avoid using * in select .






                      share|improve this answer













                      I would suggest try composite index on message Table customer_id,contractnumber and check performance, try to avoid using * in select .







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 days ago









                      vishalvishal

                      268




                      268





















                          0














                          I think I've finally found the answer to this. My sample table creation code above did not show the character encoding (a learn for me, don't shortcut sample code!). As it turns out, the customercontacts table is encoded in utf8 while the messages table is encoded in utf8mb4.



                          When columns of different character encoding are joined, indexes cannot be used.



                          This answers the 'why is this happening'. To fix this, we modified the utf8m4 table to downgrade the encoding of the indexed column to utf8 (while keeping the encoding of the rest of the table as utf8mb4). This has fixed performance for us.






                          share|improve this answer





























                            0














                            I think I've finally found the answer to this. My sample table creation code above did not show the character encoding (a learn for me, don't shortcut sample code!). As it turns out, the customercontacts table is encoded in utf8 while the messages table is encoded in utf8mb4.



                            When columns of different character encoding are joined, indexes cannot be used.



                            This answers the 'why is this happening'. To fix this, we modified the utf8m4 table to downgrade the encoding of the indexed column to utf8 (while keeping the encoding of the rest of the table as utf8mb4). This has fixed performance for us.






                            share|improve this answer



























                              0












                              0








                              0







                              I think I've finally found the answer to this. My sample table creation code above did not show the character encoding (a learn for me, don't shortcut sample code!). As it turns out, the customercontacts table is encoded in utf8 while the messages table is encoded in utf8mb4.



                              When columns of different character encoding are joined, indexes cannot be used.



                              This answers the 'why is this happening'. To fix this, we modified the utf8m4 table to downgrade the encoding of the indexed column to utf8 (while keeping the encoding of the rest of the table as utf8mb4). This has fixed performance for us.






                              share|improve this answer















                              I think I've finally found the answer to this. My sample table creation code above did not show the character encoding (a learn for me, don't shortcut sample code!). As it turns out, the customercontacts table is encoded in utf8 while the messages table is encoded in utf8mb4.



                              When columns of different character encoding are joined, indexes cannot be used.



                              This answers the 'why is this happening'. To fix this, we modified the utf8m4 table to downgrade the encoding of the indexed column to utf8 (while keeping the encoding of the rest of the table as utf8mb4). This has fixed performance for us.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 20 hours ago

























                              answered yesterday









                              Chris KnightChris Knight

                              154




                              154





















                                  -1














                                  I'd consider adding an INTEGER "id" field as the primary key for customercontracts and then reference through customercontracts_id instead of contractnumber in the messages table. You should see an improvement in performance already.



                                  Alternatively, you can try adding FULLTEXT indexes on both contractnumber columns, but I'd advise for the first option.






                                  share|improve this answer








                                  New contributor




                                  T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.
























                                    -1














                                    I'd consider adding an INTEGER "id" field as the primary key for customercontracts and then reference through customercontracts_id instead of contractnumber in the messages table. You should see an improvement in performance already.



                                    Alternatively, you can try adding FULLTEXT indexes on both contractnumber columns, but I'd advise for the first option.






                                    share|improve this answer








                                    New contributor




                                    T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






















                                      -1












                                      -1








                                      -1







                                      I'd consider adding an INTEGER "id" field as the primary key for customercontracts and then reference through customercontracts_id instead of contractnumber in the messages table. You should see an improvement in performance already.



                                      Alternatively, you can try adding FULLTEXT indexes on both contractnumber columns, but I'd advise for the first option.






                                      share|improve this answer








                                      New contributor




                                      T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.










                                      I'd consider adding an INTEGER "id" field as the primary key for customercontracts and then reference through customercontracts_id instead of contractnumber in the messages table. You should see an improvement in performance already.



                                      Alternatively, you can try adding FULLTEXT indexes on both contractnumber columns, but I'd advise for the first option.







                                      share|improve this answer








                                      New contributor




                                      T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      share|improve this answer



                                      share|improve this answer






                                      New contributor




                                      T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      answered 2 days ago









                                      T. WayneT. Wayne

                                      1




                                      1




                                      New contributor




                                      T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.





                                      New contributor





                                      T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.






                                      T. Wayne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023481%2fmysql-poor-select-query-performance-despite-indexes%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                                          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                                          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved