Archive

Archive for the ‘Coding’ Category

Moving wordpress tables

August 22nd, 2009 staze Comments off

This also applies to renaming table prefixes within the same database.

Previously on my server, I had 3 databases that I used actively. One for this website, one for another, and one for my power/gas data. The two websites are both wordpress.

So, just a few moments ago, I consolidated those tables into one database. I had to change the table prefix for one of the sites so both sites tables could coexist within the same database (and not have collisions). After changing the wp-config.php to reflect this change, the site loaded up. The problem is, I tried to go into the admin page, and I get “You do not have sufficient permissions to access this page”.

A lot of the info on the wordpress site doesn’t really cover this issue. Luckly, this does. Basically, you need to go into the “usermeta” table, and change the “meta_key” values from the previous prefix_capabilities, and prefix_user_level, to the new prefix_capabilities and new prefix_user_level. Then go into the options table and change prefix_user_roles to the new prefix_user_roles.

Once that’s done, things should work. You can certainly use the SQL statements he has on his site, but I had a hard time following what he was saying. So, I did it by hand once I figured it out.

Thanks very much, and hopefully I’ll have more later tonight or tomorrow.

(p.s. Sorry about the quality of the camera image. I’m working on it…)

Categories: Coding Tags: moving tables, MySQL, Wordpress

MySQL geekout

August 14th, 2009 staze Comments off

So, I’ve spend the last week or so adding some new features to the site. Mainly for my power usage, but you’ll also notice gas usage info now.

Anywho, to do all this, I’ve been digging into MySQL, which until now I knew enough to do a "select * from table where id=x". I knew really nothing about query optimization, how to find out how long a query took, etc.

So, after some reading, I find out about “explain”. Okay, so I start looking through queries, and digging around for answers.

First, I’ll say the system MySQL is running on it a G4, 1.67ghz. 2gig of ram, and mysql’s main settings are:

skip-locking
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
thread_cache_size = 8
query_cache_size = 16M
myisam_sort_buffer_size = 64M
thread_concurrency = 2
skip-thread-priority

Now, I am sure these aren’t ideal… but it’s what I’ve gotten based on one of the template my.cnf files, as well as some googling.

Examples: 200k row table. Each row has 8 columns. One primary key on id, and another index on datetime.

Query 1: "select datetime,kw,temp from power where date(datetime)=20090802;"
Query takes, 0.28 seconds, returns 1440 rows. Explain tells me it’s doing a simple “where”, looking at 200k rows, but that it’s not using a key, or index. Hmm… okay.
Query 2: "select datetime,kw,temp from power where datetime >= 20090802 and datetime < date(20090802 + interval 1 day);"
Query takes, .03 seconds, returns 1440 rows. Explain tells me it's also doing a where, but it's using the datetime index, and only looking at 1363 rows (no idea why that's less than 1440). Huh.

So, question. Why is MySQL not smart enough to realize that the first query should be equal to the second? Who knows... lot of people ask the question online though, why "date(datetime)" doesn't use the "datetime" index. *shrugs*

The other issue I ran into is finding all the dates in the table, in a way that first finds years, then months for those years, then days for those months. Previously, I was doing three queries against the 200k row table, that went something like:

"select distinct year(datetime) from power;"
"select distinct month(datetime) from power where year(datetime)=$year;"
"select day(datetime) from power where month(datetime)=$month and year(datetime)=$year;"

I can hear you cringe from here. It's horrible, but it worked. Each query obviously looked at all 200k rows, and took anywhere from .6-.8 seconds.

So, needless, all three of those took about 2-3 seconds to run, concurrently. ugh. So, what to do? I couldn't think of a good way to speed those times up much with just the one 200k row table, so, I created another table. This table simply holds dates (in date format, as well as year, month, and day), and then daily summaries. So I can easily grab data from this table with either a full date, or just the year, month, or day. I then have indexes for date, year, month, day, and (year,month). Now, when I do the above queries (without the year(), month(), or day() functions) I get results in basically 0.00 seconds (that's what mysql tells me anyway. So, all three running results in basically an instant set of days, months, and years. So, the archive page for power loads all but instantly now, where it used to take a couple seconds.

Back when I first switched to mysql, I figured I was going to have to do this, since it just seemed the logical way to do it. I had a table already set up, I just wasn't populating it (I need to figure out triggers now, so maybe I can have mysql do the population on its own). And ultimately, it's how I handled my flat file setup before. There was a "history" file, and then individual day files. *sigh* Takes figuring something out manually in order to kick someone out of being lazy. I mean, with 200k rows, it was being slow. What happens when I have a year worth of data (about 526k rows). Where, a year worth of data in this summary table is only what, 365 (so, one would expect queries to be about 1500x faster, if things worked linearly) rows?

So, that's it. That's what's kept me busy the past few days. I still have some more stuff I want to add, but I'm pretty happy at this point. Things are faster, and all and all, my server shouldn't come crashing down now when google spiders my site. =)

Have a great weekend. Hopefully I'll post more by Sunday.

UPDATE:
Just made a change to the daily graph system. Since my work yesterday, it was not easy to get to todays power usage info, because the dateselect.php was grabbing info from the summary table, which only contained days in the past. I've just changed how that table is updated. Now, nightly, the previous days row is updated, and the current day is added to the table with just the date, year, month, and day. So, it'll now show up in the dateselect. I think, now that I think about it, I'm going to add a link to the front page for todays info.

More later.

Categories: Coding Tags: MySQL

House guest, Sysadmin day, and moving from flat files to MySQL

August 1st, 2009 staze Comments off

The last week Tara’s sister has been staying with us. It’s actually been pretty fun having an extra person in the house. I wouldn’t have thought the house too big for 2 people, but it does seem more homey with another (except maybe the kitchen that’s a bit crowded). She heads back home tomorrow, but it has indeed been fun. We’re attempting to make it a routine thing for her to come down during the summers, as she gets to do whatever she wants (within reason) here, on her own time. It’s a “vacation” of sorts.

Yesterday was Systems Administrator day (http://www.sysadminday.com/), so I decided to take the day off and hang out at home. Aside from driving Tara and her sister around (which was fine), I finally picked Fallout 3 back up (can’t wait for the PS3 DLC). Finished off a mission, and realized that at level 20, there doesn’t seem to be a lot of “threats” in the wasteland. Once Broken Steel drops for the PS3, it’ll just make me that much more burly.

On the “work” side, I finally moved my power monitoring system over to mysql from flat files. I’m still working on updating all the scripts that interact with that data, but the front page “widget” works. I have an interface nearly ready for looking at past days data. Currently, at just over 4 months of data (about 175k rows), the DB is sitting at 12MB. So, a year’s worth of data should be about 36MB, or about half a million rows. That’s with 3 keys, for row id, house code (incase I ever add another panel monitor), and datetime. I think reading a single flat file might be a bit faster, but MySQL gives some much more powerful abilities, like showing all the data for a month, or seeing high and low temps for a given day, easily. It’s certainly teaching me SQL select statements.

I’ll post again when I have everything working again, and have added some new features to the data. I hope, shortly after that, I’ll be able to post the code in my “code” section. But that requires I do some commenting within the code first. I’m pretty bad at commenting code (I never got in the habit), so going back to previous code always takes a few minutes of trying to figure out what I was doing.

Have a great weekend, and next week. I’m hoping for some T-Storms.

First Google Visualizations API usage

May 7th, 2009 staze Comments off

So, I finally did some basic Google Visualization stuff. On the sidebar, for my energy usage, you can now see a link for usage history. This will open a new window that shows daily KWH usage for my house. It’s an annotated timeline, though I’m not currently doing annotations.

It’s pretty simple, about 40 lines total, of which, 15 or so are php. Nothing too special. You’ll also find a link on that page to the “RAW” data, in all it’s “glory”.

More to post later, but for now, it’s all I got.

Here’s the page I used for how to structure the java output from php to make it work: http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html

Take care!

Categories: Coding, Energy Tags: API, google, KWH, PHP, TED, visualizations