Jump to content

locked tables?


acidglitter

Recommended Posts

i just closed down my site for a couple hours to remake one of the tables. i put the fields selected the most often in one table and more specific things in another table. i was hoping that might help if theres too much stress on the tables. then i put the site back online and after pretty much 3 people viewed the home page the whole site stopped loading all over again! i don't know why this is happening at all. :( :( :(

Link to comment
Share on other sites

because i don't understand them. but i will try looking up tutorials and stuff for them today. my server is saying that the tables on my site are locked because of the way the queries are written. would anyone be able to help me rewrite my queries? or would it probably fix everything if my table had indexes?

Link to comment
Share on other sites

because i don't understand them. but i will try looking up tutorials and stuff for them today. my server is saying that the tables on my site are locked because of the way the queries are written. would anyone be able to help me rewrite my queries? or would it probably fix everything if my table had indexes?

You're doing a full table scan for EVERY query, so of course your tables are locked all of the time.  There are many good sites linked in the stickes on thsi forum.  Nothing fixes everything.

Link to comment
Share on other sites

well i went through my queries and realized that this was being done a lot..

DATEDIFF(DATE(vip), CURDATE())

on one of the pages it actually happened 3 times in one query. so i made a page that still uses that, but then gets the actual days left from that and updates a new field called daysleft (with an INT type). that page will only execute 4 times a day (by a cronjob) to keep it up to date. then i put an index on daysleft, and now the rest of my queries are using that field instead of that code above. so i'm HOPING that this will fix my problem..!! does anyone have any suggestions? the rest of my queries are pretty simple so thats the only thing that seems like it could be causing the tables to lock.

Link to comment
Share on other sites

i'm not really sure which pages are causing the problems, or if its all of them combined, so these are just queries from two pages i think are probably viewed the most..

 

SELECT friendid, picture, name, daysleft
FROM members WHERE daysleft > 0
ORDER BY daysleft DESC

 

SELECT friendid, name, picture, points,
daysleft as 'vip'
FROM members
WHERE friendid NOT IN
	(SELECT theirs FROM added WHERE yours='$friendid')
ORDER BY (daysleft > 0) DESC, points DESC
LIMIT $limit

 

SELECT points,addedtoday,
(SELECT COUNT('yours') FROM added WHERE theirs='$friendid') AS 'requests'
FROM members_detailed WHERE friendid='$friendid'

 

 

Link to comment
Share on other sites

UGH i don't know what else to do. :(

We can help you deal with one query at a time... like for instance, why is VIP not a DATE column to begin with? That's the slow part... and having a cron job isn't the answer.

 

It's unlikely that's the only issue... turn on the slow-query-log and you'll see..

Link to comment
Share on other sites

  • 2 weeks later...

UGH i don't know what else to do. :(

We can help you deal with one query at a time... like for instance, why is VIP not a DATE column to begin with? That's the slow part... and having a cron job isn't the answer.

 

It's unlikely that's the only issue... turn on the slow-query-log and you'll see..

 

 

vip is a date column. what i did was try to get how many days from now until the saved date and turn that into just a number. i've talked to my server before and there's no way i could get a slow-query-log to work. i'm hoping there are other ways to find the problem.. though it does seem like my site is down less often since i added the indexes

Link to comment
Share on other sites

okay, for the same 3 queries i put above, in the same order, this is what explain says...

 

id select_type table type possible_keys key key_len ref rows Extra

1 SIMPLE members range daysleft daysleft 4 NULL 142 Using where

 

 

id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY members ALL NULL NULL NULL NULL 49561 Using where; Using filesort

2 DEPENDENT SUBQUERY added ALL NULL NULL NULL NULL 2612 Using where

 

 

id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY members_detailed const PRIMARY PRIMARY 8 const 1

2 SUBQUERY added ALL NULL NULL NULL NULL 2616 Using where

Link to comment
Share on other sites

The first query is good -- you see how there's no filesort, and the key isn't NULL?

 

The second query is bad for two reasons: first, there's no index on the members (table scan = ALL)... second, you're using a dependent subquery, which si executed once for EVERY matching row of members -- or ~2600 times!  Could you explain what you're trying to query?

 

For the 3rd one you still have no index usage -- add an index on friendID for the members_detailed table, and add a composite index for ( theirs, yours ) to added.

 

Also, your queries would really benefit from table aliases and column prefixes.

Link to comment
Share on other sites

  • 2 weeks later...

first thanks for your replies. your help has been very useful!

 

and then, i added an index to yours and theirs on added, so the 2nd and 3rd queries now look like..

 

SELECT friendid, name, picture, points,
daysleft as 'vip'
FROM members
WHERE friendid NOT IN
	(SELECT theirs FROM added WHERE yours='$friendid')
ORDER BY (daysleft > 0) DESC, points DESC
LIMIT $limit

 

id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY members ALL NULL NULL NULL NULL 50800 Using where; Using filesort

2 DEPENDENT SUBQUERY added ref yours,theirs yours 4 const 3 Using where

 

SELECT points,addedtoday,
(SELECT COUNT('yours') FROM added WHERE theirs='$friendid') AS 'requests'
FROM members_detailed WHERE friendid='$friendid'

 

id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY members_detailed const PRIMARY PRIMARY 8 const 1

2 SUBQUERY added ref theirs theirs 4 const 68 Using where; Using index

 

 

so is the 3rd query okay now? i'm not sure what to fix for the 2nd query because friendid is a primary key. the 2nd query is to select members you haven't added yet so you can add them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.