A php page contains multiple query statements, which slows down page opening. what should I do?

Source: Internet
Author: User
A php page contains multiple query statements, which leads to slow page opening. the following code is messy. let me explain it briefly. First, query the department records in the department table, then query the users in the department table based on the department ID, and then query the logs in the user table based on the user ID, then, query the audit records of the leaders in the audit table based on the log ID ......, It takes about one minute to open the page.

I guess it is caused by too many data table scans, but I don't know how to solve it?

// Query the department
$ SQL _depart = "SELECT * FROM depart WHERE gid> 0 AND gid <15 ORDER BY gid ";
$ Query_depart = $ db-> query ($ SQL _depart );
While ($ d = $ db-> fetch_array ($ query_depart )){
?>




// Query personnel

$ SQL _user = "SELECT * FROM members WHERE flag <5 and groupid = '". $ d ['gid']. "'Order BY flag desc ";
$ Query_user = $ db-> query ($ SQL _user );
While ($ u = $ db-> fetch_array ($ query_user )){
?>







// Query all logs
$ SQL = "SELECT c1. *, c2. * FROM log c1 LEFT JOIN log_time c2 ON (c1.time _ id = c2.tid) WHERE c2.year = '". $ year. "'AND c1.userid = '". $ u ['uid']. "'AND c2.end _ date <'". date ("Y-m-d "). "'Order BY c2.tid desc ";
$ Query = $ db-> query ($ SQL );
While ($ row = $ db-> fetch_array ($ query )){

?>






The index is created, and the result does not work at all.

If ?? No ??? Can be used? Save.

For example:
$ Data = getcache ();//?? Saving? Fetch
If ($ data = ''){
//? Db? Take ??
$ Data = '? Db? ';
Setcache ($ data );//? Incoming? Storage
}

Echo $ data;

Check whether the index is used,
Minimize the amount of content that does not need to be queried, or minimize the number of items that need to be queried at a time.




Explain your SQL statement to check the index usage








You can store data in files or memcache.


However, you cannot query in a loop. You can query and recycle all the data in two tables at a time, which may be faster than the current one.

The nested loop query is terrible.

In my opinion, the nested loop query efficiency is very low. we recommend that you merge it.

Optimize the SQL statement. try to find all the content in one SQL statement, and then demonstrate it cyclically below.

Association: Department table associated user table associated log table associated audit table
Sort: first sort by Department id, then by user id, then... (the relationship behind is not clear)
Condition: those of you. integrate them.
Query field: used only, such as Department id, department name, user id, user name)
Number of queries: You do not have one. we recommend that you write one. it is terrible to query big data without paging.
In this way, only one statement can be used to obtain data.
The next step is to traverse the data. The data is sorted in ascending order, so you don't have to worry about chaos.
First, traverse the department. if the department id is different, create another table.
Each department traverses the user and creates another table if the user is different.
Every user...

I don't know if the following SQL statement can speed you up (it is not guaranteed that the SQL statement is correct and feasible)

SELECT d. *, m. *, l. * FROM depart d
Left join members m on m. flag <5 and m. groupid = d. gid
Left join (SELECT c1. *, c2. * FROM log c1 left join log_time c2 ON (c1.time _ id = c2.tid) WHERE c2.year = '". $ year. "'AND c2.end _ date <'". date ("Y-m-d "). "') l on l. userid = m. uid
WHERE d. gid> 0 AND d. gid <15 order by d. gid, m. flag desc, l. tid desc

Written as a statement ~

Desc SQL
Create an index using the keyword field










If ?? No ??? Can be used? Save.

For example:
$ Data = getcache ();//?? Saving? Fetch
If ($ data = ''){
//? Db? Take ??
$ Data = '? Db? ';
Setcache ($ data );//? Incoming? Storage
}

Echo $ data;


If ?? No ??? Can be used? Save.

For example:
$ Data = getcache ();//?? Saving? Fetch
If ($ data = ''){
//? Db? Take ??
$ Data = '? Db? ';
Setcache ($ data );//? Incoming? Storage
}

Echo $ data;

I don't know if the following SQL statement can speed you up (it is not guaranteed that the SQL statement is correct and feasible)

SELECT d. *, m. *, l. * FROM depart d
Left join members m on m. flag <5 and m. groupid = d. gid
Left join (SELECT c1. *, c2. * FROM log c1 left join log_time c2 ON (c1.time _ id = c2.tid) WHERE c2.year = '". $ year. "'AND c2.end _ date <'". date ("Y-m-d "). "') l on l. userid = m. uid
WHERE d. gid> 0 AND d. gid <15 order by d. gid, m. flag desc, l. tid desc

Association: Department table associated user table associated log table associated audit table
Sort: first sort by Department id, then by user id, then... (the relationship behind is not clear)
Condition: those of you. integrate them.
Query field: used only, such as Department id, department name, user id, user name)
Number of queries: You do not have one. we recommend that you write one. it is terrible to query big data without paging.
In this way, only one statement can be used to obtain data.
The next step is to traverse the data. The data is sorted in ascending order, so you don't have to worry about chaos.
First, traverse the department. if the department id is different, create another table.
Each department traverses the user and creates another table if the user is different.
Every user...

Reply to discussion (solution) Is this written to a file?Is there a complete instance for your reference? Is this written to a file?Is there a complete instance for your reference? Yes? In the file. Http://www.itstrike.cn/Question/PHP-technology-to-achieve-dynamic-cache-instance I tried it. the query efficiency is fast, so I don't need to wait. but how can I call it on the page? My requirement is to first display the department, then all users in the department, and so on. But now it is displayed together with departments, users, and others. First, traverse the department. if the department id is different, create another table.Each department traverses the user and creates another table if the user is different. What does it mean to create another table with different IDs?


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.