Ajax Chat Queries locking.. Help

Discussion in 'MySQL' started by PhilG, Jan 24, 2011.

  1. #1
    Hi,

    I'm trying to optimize my online chat and this query show up because in the log becuase it is constantly locking the table.

    SELECT
                                                    userID,
                                                    userName,
                                                    userRole,
                                                    channel,
                                                    UNIX_TIMESTAMP(dateTime) AS timeStamp,
                                                    ip
                                            FROM
                                                    ajax_chat_online
                                            ORDER BY
                                                    userName;
    
    Code (markup):
    Explain:
    id select_type table type possible_keys key key_len ref rows Extra
    1 SIMPLE ajax_chat_online ALL NULL NULL NULL NULL 6 Using filesort

    I tried to add a key to userName but it is still using filesort which is locking the table.

    How would you optimize this query/table?
     
    PhilG, Jan 24, 2011 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    hey, what is the data size? As in how many columns the table has and how many rows?

    Also, I have noticed no filter option in your query. Do you need to read all the data in table and display?
     
    mastermunj, Jan 25, 2011 IP
  3. PhilG

    PhilG Active Member

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Usually the table contains about 5-20 rows. It is the table that hold the users who are online at the time. When someone is in chat this query is run reguarly to show who is still online. It is locking contantly so I am trying to optimize. I tried adding userName as an index but that did not help.
     
    PhilG, Jan 25, 2011 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    With just 20 rows, chances are very very less that table will get locked. However, checking following points would help:

    1. Is table used in JOIN query anywhere in app?
    2. Does table have constant select / insert / update / delete queries running over it?
    3. Did you see process list when the table gets locked? There could be something else which is locking table. May be a heavy process using most of resources or something like that.
     
    mastermunj, Jan 25, 2011 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    That query shouldn't lock the table unless there are inserts waiting to be committed. It would normally require some pretty extreme load to actually see a delay.

    What engine are you using? How many users are online at the same time.
     
    jestep, Jan 26, 2011 IP
  6. PhilG

    PhilG Active Member

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    60
    #6
    thanks for all the replies. I think that the problem is either mysql is'nt configured very well or there are other queries running before it and locking things. I am using MyISAM, the forum has about 60 members logged in on average.

    How would I find the query that is causing other queries to lock up?
     
    PhilG, Jan 26, 2011 IP