arthurakay
09-30-2011, 08:49 PM
I'll start by saying that I am the furthest thing from a database admin... my knowledge is SQL queries is basic at best, which is why I'm asking for help here.
I am attempting to build a "search" query on a client's message board (running vBulletin). The query I've written works fine - with the exception that it's terribly slow (if I set the start/limit params to 0 an 25, the query takes upwards of 30-60 seconds).
Here's my query (a mix of PHP and raw SQL):
$totalQuery = '
SELECT DISTINCT
thread.threadid,
thread.title,
thread.lastpost,
thread.lastposter,
thread.forumid,
thread.postusername,
thread.postuserid,
thread.dateline
FROM thread
RIGHT JOIN
(
SELECT
post.threadid,
post.pagetext
FROM post
) AS post
ON thread.threadid = post.threadid
WHERE thread.forumid NOT IN
(
SELECT forum.forumid
FROM forum, forumpermission
WHERE FIND_IN_SET(forumpermission.forumid, forum.parentlist)
AND forumpermission.usergroupid >= "' . $_SESSION['perms']['permissions']->usergroupid . '"
)
AND
(
thread.title LIKE "%' . mysqli_real_escape_string($mysql, $args['querystring']) . '%"
OR post.pagetext LIKE "%' . mysqli_real_escape_string($mysql, $args['querystring']) . '%"
)
ORDER BY lastpost DESC
LIMIT ' . $start . ',' . $limit;
So the idea is that I'm searching through both the post TITLE and post CONTENT for anything close to the provided query. However, I also need to make sure that the user only gets results that they have permissions to see... I imagine this probably causes the problem.
There has to be a better way to optimize the query... all suggestions will be appreciated.
I am attempting to build a "search" query on a client's message board (running vBulletin). The query I've written works fine - with the exception that it's terribly slow (if I set the start/limit params to 0 an 25, the query takes upwards of 30-60 seconds).
Here's my query (a mix of PHP and raw SQL):
$totalQuery = '
SELECT DISTINCT
thread.threadid,
thread.title,
thread.lastpost,
thread.lastposter,
thread.forumid,
thread.postusername,
thread.postuserid,
thread.dateline
FROM thread
RIGHT JOIN
(
SELECT
post.threadid,
post.pagetext
FROM post
) AS post
ON thread.threadid = post.threadid
WHERE thread.forumid NOT IN
(
SELECT forum.forumid
FROM forum, forumpermission
WHERE FIND_IN_SET(forumpermission.forumid, forum.parentlist)
AND forumpermission.usergroupid >= "' . $_SESSION['perms']['permissions']->usergroupid . '"
)
AND
(
thread.title LIKE "%' . mysqli_real_escape_string($mysql, $args['querystring']) . '%"
OR post.pagetext LIKE "%' . mysqli_real_escape_string($mysql, $args['querystring']) . '%"
)
ORDER BY lastpost DESC
LIMIT ' . $start . ',' . $limit;
So the idea is that I'm searching through both the post TITLE and post CONTENT for anything close to the provided query. However, I also need to make sure that the user only gets results that they have permissions to see... I imagine this probably causes the problem.
There has to be a better way to optimize the query... all suggestions will be appreciated.