MySQL, select *, limit

Source: Internet
Author: User

I wrote it a few months ago. It was messy and it was released from my mailbox.

 

 

 

Mysql select

It is said that when writing a SELECT statement, try to write the name of the selected column instead of using select *. In fact, simple tables do not matter. However, when table join or complex query is involved, this item will be effective. Assume there are four tables: t_A ID B _id col1 col2 created_by created_att_ B ID c_id col1 col2t_c ID col1 col2 col3 col4t_a There are 100,000 records T_ B there are 10,000 records T_c there are 100 records there is a query like this: select * From T_A A, T_ B B, T_c cwhere. created_by <1000and. B _id = B. idand B. c_id = C. idorder by. created_at DESC assume that there are 100 results in the query, and the fields required for the query results are incomplete. At this time, "*" is used for independent writing, which can be of great impact. Cause: This query is a Cartesian product first to generate a temporary table. If select * is used, the number of columns in this temporary table is the sum of the columns in the three tables: 6 + 4 + 5 = 15. However, if select a. *, B. * is used, the list of this temporary table is the number of columns in the select clause, plus the number of attributes used in the WHERE clause. In addition, because of the number of columns, the memory space occupied by the generated table is different, such as whether to add a varchar or field. When the memory size configured for the database server cannot be fully loaded, you need to write the temporary table to the disk space and perform subsequent operations. In addition, the limit function of MySQL seems to be quite simple to implement. In case of a query, the limit statement is different. Select * From XXXXX where XXXXXXXXX limit 0, 20 select * From XXXXX where XXXXXXXXX limit 100000, 20 the two queries, the latter one needs to write the temporary table to the disk space, not the first one...
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.