Use MySQL temporary tables to accelerate queries

Source: Internet
Author: User

MySQL temporary tables can be used to accelerate queries. The following describes how to use MySQL temporary tables to accelerate queries.

Sort a subset of a table and create a MySQL temporary table, which sometimes accelerates query. It helps avoid multiple sorting operations and simplifies the optimizer's work in other aspects. For example:

 
 
  1. SELECT cust.name,rcVBles.balance,……other columns  
  2.  
  3. SELECT cust.name,rcVBles.balance,……other columns  
  4.  
  5. FROM cust,rcvbles  
  6.  
  7. WHERE cust.customer_id = rcvlbes.customer_id  
  8.  
  9. AND rcvblls.balance>0  
  10.  
  11. AND cust.postcode>“98000”  
  12.  
  13. ORDER BY cust.name   

If this query is executed multiple times but more than once, you can find all the unpaid customers in a temporary file and sort them by customer name:

 
 
  1. SELECT cust.name,rcvbles.balance,……other columns  
  2.  
  3. SELECT cust.name,rcvbles.balance,……other columns  
  4.  
  5. FROM cust,rcvbles  
  6.  
  7. WHERE cust.customer_id = rcvlbes.customer_id  
  8.  
  9. AND rcvblls.balance>0  
  10.  
  11. ORDER BY cust.name  
  12.  
  13. INTO TEMP cust_with_balance   

Then, query in the temporary table in the following way: SELECT * FROM cust_with_balance

WHERE postcode> 98000"
The temporary table has fewer rows than the primary table, and the physical order is the required order, which reduces disk I/O, so the query workload can be greatly reduced.

Note: after a temporary table is created, the modification to the primary table is not reflected. Do not lose data when the data in the master table is frequently modified.


 

How to view MySQL binary logs

Mysql Log File description

How to relocate the MySQL DATA DIRECTORY

MySQL database table Representation

Location of MySQL DATA DIRECTORY

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.