webdevRefinery Forum: Best way of doing this (minimum queries / fastest set of queries) - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline euantor 

  • Group: Members
  • Posts: 24
  • Joined: 22-November 11
  • LocationNorth East England
  • Expertise:HTML,CSS,PHP,Java,Javascript,SQL

Posted 08 February 2012 - 11:44 AM (#1)

Best way of doing this (minimum queries / fastest set of queries)


Here's the scenario, I'm building a status update system for a piece of forum software (MyBB). It's been requested that the system should allow comments to be posted on each status update. That's fine. The current structure is this:

statuses

sid - INT(10) - PRIMARY KEY
uid - INT(10) - FOREIGN KEY
status - TEXT
dateline - BIGINT(30)
tweetid - VARCHAR(20)

________

status_comments

cid - INT(10) - PRIMARY KEY
sid - INT(10) - FOREIGN KEY
comment - TEXT
dateline - BIGINT(30)


The thing is, it's required that the 10 most recent statuses are shown on a page - along with the comments for each status. Obviously, I need to run a query to get the 10 latest statuses. I could then run a query for each of these ten statuses to get their related comments, but that seems like an awfully long-winded way of doing it.

I'm really not the best at SQL, so I was wondering if any gurus here might know of a handy little query I may be able to run?
0


User is offline AwesomezGuy 

  • Certified Asshole™
  • Group: Members
  • Posts: 1245
  • Joined: 08-March 10
  • LocationIreland
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 08 February 2012 - 01:02 PM (#2)

You want to use a LEFT JOIN for this, Google it. :)
0


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3415
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 08 February 2012 - 05:24 PM (#3)

A left join would work, you'd get a lot of data back from the database though (it'd return duplicates of every status - One for each comment). That's quite a bit of overhead, especially if your database is a separate server to your web server.

Here's how I'd do it with two queries (PHP-ish, I can't remember the exact syntax for PDO queries :P):
// First get the statuses
$statuses = array();
$statusResult = $db->query('
	SELECT sid, uid, status, dateline, tweetid
	FROM statuses
	ORDER BY sid DESC
	LIMIT 10');
	
while ($status = $statusResult->fetchObject())
{
	// Create empty array for comments
	$status->comments = array();
	
	$statuses[$status->sid] = $status;
}

// Get the comments for all these statuses
$statusIds = array_keys($statuses);
$commentResult = $db->query('
	SELECT cid, sid, comment, dateline
	FROM status_comments
	WHERE sid IN (' . implode(', ', $statusIds) . ')');
	
while ($comment = $commentResult->fetchObject())
{
	$statuses[$comment->sid]->comments[$comment->cid] = $comment;
}

// $statuses now has every status, with all their comments


The second query gets the comments for just the statuses retrieved.

Then in your view/template file:
foreach ($statuses as $status)
{
	// Output the status
	echo '
	<li>
		', $status->status, '
		...
		<ol class="comments">';
		
	foreach ($status->comments as $comment)
	{
		echo '
			<li>
				', $comment->comment, '
				...
			</li>';
	}
		
	echo '
		</ol>
	</li>'
}

Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline euantor 

  • Group: Members
  • Posts: 24
  • Joined: 22-November 11
  • LocationNorth East England
  • Expertise:HTML,CSS,PHP,Java,Javascript,SQL

Posted 08 February 2012 - 08:02 PM (#4)

@Daniel: Now, why did I not think of that? Of course that's the best way to do it! Wow. You just blew my mind, literally. Thanks! Sometimes I just see the wood for the trees.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services