I have a table I need to perform a horrible query on (not my db design).
The table is simple:
`id` bigint(255) NOT NULL AUTO_INCREMENT,
`poster` varchar(30) NOT NULL,
`chattext` varchar(255) NOT NULL,
`timeposted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`type` int(3) NOT NULL DEFAULT '0',
`towho` varchar(30) DEFAULT NULL
My current query does what I want, but at a horrible cost to the server. I'm running near max CPU with only a handful of users. Sadly, every 5 seconds they do this pull.
Current query:
SELECT c.poster, c.chattext, c.type, c.towho, c.timeposted, u.utype, u.locz
FROM chat c
LEFT JOIN users u ON c.poster=u.name
WHERE c.type!=4
UNION (
SELECT c.poster, c.chattext, c.type, c.towho, c.timeposted, u.utype, u.locz
FROM chat c
LEFT JOIN users u ON c.poster=u.name
WHERE c.type=4 AND c.towho='USERNAME')
UNION (
SELECT c.poster, c.chattext, c.type, c.towho, c.timeposted, u.utype, u.locz
FROM chat c
LEFT JOIN users u ON c.poster=u.name
WHERE c.type=4 AND c.poster='USERNAME')
ORDER BY timeposted DESC LIMIT 0, 25"
This gets performed every 5 seconds by all users online. As you can see, it quickly becomes a resource hog.
I'm used to MSSQL so I should be able to grasp the concepts for MySql, and syntax hasn't been too different. This query was given to me without the user tables added, so I think my mindset is stuck in making this work over finding a better way.
I think I'm doing this the wrong/complicated way. So any assistance in improving performance is appreciated.