Does the limit of mysql affect the selection of indexes used by sql?

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

Enter image description

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

Enter image description

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

Enter image description

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

Enter image description

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?

Enter image description

Enter image description

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

Enter image description

Enter image description

Try using the mandatory index mentioned in the topic

Enter image description

Later, I thought about whether it is possible to do a nested query to achieve the same effect of selecting enterpriseId

Enter image description

Sure enough! learned! Troubleshooting is also a great learning process!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117779&siteId=291194637