Posts Tagged ‘SQL’

Wordpress mySQL troubles

Thursday, May 6th, 2010

This was an issue caused by my own carelessness.  I have a script to just do a tar backup of my entire system to an external drive every night.  The script is this:

#!/bin/bash
OF=/mnt/backup/backup$(date +%Y%m%d%H%M%S).tar
mount /dev/sdb1 /mnt/backup
rm /mnt/backup/log.txt
touch /mnt/backup/log.txt
tar cvfW $OF / –exclude /mnt/backup –exclude /mnt/backup2 –exclude /proc –exclude /sys –exclude /dev –exclude /tmp > /mnt/backup/backup.log
find /mnt/backup/ -mtime +10 -exec rm {} \; > /mnt/backup/cleanup.log
umount /dev/sdb

It mounts the external drive, then backs up the system drive, then removes old backups.  Pretty simple.

If the external drive is not present to be mounted, tar continues on to do the backup to the /mnt/backup directory, even if the drive is not mounted at that directory.

So last night, I removed the drive for another purpose, since nothing has changed on my system in the past day.

Well, the backup process proceeded and filled my hard drive.  Luckily, it did not completely crash, and honestly, I probably wouldn’t have immediately noticed had I not needed to update a page for one of my client’s sites.  After uploading the page to the server, I noticed it was simply displaying a blank page.  I uploaded again and noticed filesize kept saying 0kb.  I proceeded to check things out and discovered that my hdd was full.  After some digging, I realized my mistake.  I removed the backup and life things returned to normal… almost.

My wordpress installation now showed all comments, tags, plugins, everything… except for the posts.  I was confused.  Everything seemed fine.  I decided to take a look at the mySQL database.

To login to your local mySQL db:

mysql -uroot –password=<root password> <databasename>

At the mySQL command prompt, I simply tried to see what I could pull from the posts table(for a diagram of wordpress database, see this.)

select * from wp_posts;

This returned the error:

“ERROR 145 (HY000): Table ‘<table name>’ is marked as crashed and should be repaired”

So, I tried a quick repair on the table:

repair table wp_posts;

and all was good in the world.

Find Tables Missing Indexes and Create Clustered Indexes for Them

Wednesday, February 24th, 2010

**Update at bottom**

OK, so we had a SQL 2005 database that we migrated from another company.  The application that uses it as a backend was having some terrible performance issues.  We had limited information on the previous configuration, so we bumped the Application up to a newer server since it seemed most of the load issues were with that part.  Afterwards, there were still performance issues and timeouts.  SQL was queuing up commands and taking too long to process them.  So we got the senior DBA involved to help us see what kind of performance increases we could get on the SQL server.  Unfortunately, I did not note all of the changes, but this was the biggest improvement.

We found out that the previous company also had performance issues.  When looking at the tables, we noticed many did not have indexes.  This little query was a lifesaver.  It shows you which tables are without an index, how many reads/writes and if things are queuing.

SELECT

migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) AS improvement_measure,

‘CREATE INDEX [missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle)

+ '_' + LEFT (PARSENAME(mid.statement, 1), 32) + ']‘

+ ‘ ON ‘ + mid.statement

+ ‘ (’ + ISNULL (mid.equality_columns,”)

+ CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ‘,’ ELSE ” END

+ ISNULL (mid.inequality_columns, ”)

+ ‘)’

+ ISNULL (’ INCLUDE (’ + mid.included_columns + ‘)’, ”) AS create_index_statement,

migs.*, mid.database_id, mid.[object_id]

FROM sys.dm_db_missing_index_groups mig

INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle

INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle

WHERE migs.avg_total_user_cost * (migs.avg_user_impact / 100.0) * (migs.user_seeks + migs.user_scans) > 10

ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC

The second column is a query string generated by the first query that will create indexes based on what is needed.  A note though, that query only creates standard indexes.  I recommend to change it to create clustered indexes.  They are considerably faster.  Here are the commands I used to create our indexes.

CREATE CLUSTERED INDEX cix_MASTER1_HISTORY on MASTER1_HISTORY(mrID)

CREATE CLUSTERED INDEX cix_MASTER1_DESCRIPTIONS on MASTER1_DESCRIPTIONS(mrID)

CREATE CLUSTERED INDEX cix_MASTER1_TIMETRACKING on MASTER1_TIMETRACKING(mrID)

CREATE CLUSTERED INDEX cix_MASTER2_HISTORY on MASTER2_HISTORY(mrID)

Hopefully this helps some of you out there

**Updated 3/1/2010

This query to find indexes is dynamic.  it doesn’t actually find all tables missing indexes, it finds tables that are CURRENTLY being searched without indexes and ranks them based on the performance problems they are causing.  So run this query when your SQL starts getting backed up.