MySQL: Select query execution and result fetch time increases with number of connections

后端 未结 1 975
执念已碎
执念已碎 2021-01-06 08:35

My server application makes multiple connections to MySQL through separate threads. Each connection fires a SELECT query and fetches result which the applicatio

1条回答
  •  灰色年华
    2021-01-06 08:50

    Probably each connection is doing a full table scan of profiles. Let's try to avoid that. When there are dozens of queries hitting the same table, there are locks that cause InnoDB to "stumble over itself". Either of these plans will both speed up the query and decrease the number of rows touched (hence decrease the locking). The use of the suggested "composite" index will speed up the query. But the OR gets in the way. I see two tricks to still have an index look at uniquestring, but avoid some or all of the OR.

    (      (prfls.uniquestring like 'phk5600dcc%')
       or  (prfls.uniquestring like 'phk5600dcf%')
    )
    

    OR is hard to optimize.

    Add this:

    INDEX(isconnected, isprofilepresent, uniquestring)
    

    Then...

    Plan A:

    prfls.uniquestring         like 'phk5600dc%' AND  -- note common prefix
    (      (prfls.uniquestring like 'phk5600dcc%')
       or  (prfls.uniquestring like 'phk5600dcf%')
    )
    

    This assumes you can construct that common prefix.

    Plan B (turn OR into UNION):

    ( SELECT ...
        WHERE prfls.uniquestring like 'phk5600dcc%' AND ...
        LIMIT 450 )
    UNION ALL    -- ? You may want DISTINCT, if there could be dups
    ( SELECT ...
        WHERE prfls.uniquestring like 'phk5600dcf%' AND ...  -- the only diff
        LIMIT 450 )
    LIMIT 450   -- yes, again
    

    Plan A (if practical) takes advantage of what seems to be a common starting value. Plan B works regardless, but is probably a little slower, although still a lot faster than the original.

    Other notes...

    Indexes on flags (of which you have two) are almost never used. EXPLAIN SELECT ... will probably show that neither was used. Please provide the EXPLAIN for any SELECT that needs discussion.

    A UNIQUE KEY is a KEY, so there is not need for the redundant index on USERID.

    limit 450 -- Which 450 do you want? Without an ORDER BY, the query is allowed to give you any 450. (Of course, perhaps that is fine.) (And ORDER BY would probably slow down the query.)

    My suggestions will not "solve" the problem, but they should increase the number of connections before the slow-down becomes noticeable.

    0 讨论(0)
提交回复
热议问题