A few days ago, the tester reported to me that there is a company's address book synchronization interface that does not return data, I checked the log
The corresponding access.log status code of nginx is 499, and the request timed out. There are two possibilities for data analysis from the Internet:
1. The client actively disconnects
2. The client connection is interrupted due to the server response timeout
Asked the developer of the client and they said they set the response timeout to 5 seconds, and if 5s is not returned it times out
Check the server log again
It was found that the entire interface took more than 6s
However, there are two interfaces of other services adjusted in this interface, and I really don’t know which interface is time-consuming, so I added a time-consuming analysis before and after the call of each interface, and then check it again after the operation and maintenance update is online. log
It takes 5799ms, which means that the getPersonByContacts interface is too time-consuming
I directly find the project of the service and open the xml file
<select id="getPersonByContacts" resultType="PersonDO" parameterType="java.util.Map">
SELECT
*
FROM Person WHERE enterpriseId = #{enterpriseId}
<if test="timeModified != null and timeModified != ''">
and timeModified >= #{timeModified}
</if>
ORDER BY timeModified ASC
<if test="batchNum != null and batchNum > 0">
limit #{batchNum}
</if>
</select>
Found that sql is normal sql, look at the table structure, enterpriseId and timeModified have added corresponding indexes, but why is it still so expensive?
Use sql execution plan to view
The index used is timeModified, and it has scanned 200w+ rows. When I checked the entire table with 400w+ data, I scanned half of the table... Can it be slow?
I'm wondering why timeModified is used as an index instead of enterpriseId?
When the limit is not added, enterpriseId is selected, and the number of scanned lines is only 28000+ lines. This will definitely not be slow...
Is it the limit that affects the choice of index? If you are indecisive, you still have to ask Du Niang
https://www.zhihu.com/question/26125418/answer/32246645
Try using the mandatory index mentioned in the topic
Later, I thought about whether it is possible to do a nested query to achieve the same effect of selecting enterpriseId
Sure enough! learned! Troubleshooting is also a great learning process!