Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
In 6.5, but I think also in previous versions if there are posts in a topic from users which are no longer in the database, the posts are not displayed.

Yet the number of pages of a topic is calculated with the total number of posts.
The result is that sometimes for instance a topic has 5 pages,
but when you click on the 5th page, the page is empty.

This happens because of a too restricitve query used.

In showflat.php
this query:

Code
$query = "<br />	SELECT t1.B_Number,t2.U_Username,t1.B_Posted,t1.B_IP,t1.B_Subject,.........<br />	FROM  {$config['tbprefix']}Posts AS t1,<br />	{$config['tbprefix']}Users AS t2<br />	WHERE t1.B_Main  = $current<br />	AND   t1.B_PosterId = t2.U_Number<br />	$Viewable<br />	ORDER BY B_Number<br />	$Limit<br />";


should be:

Code
$query = "<br />	SELECT t1.B_Number,t2.U_Username,t1.B_Posted,t1.B_IP,t1.B_Subject,.........<br />	FROM  {$config['tbprefix']}Posts AS t1<br />	LEFT JOIN {$config['tbprefix']}Users AS t2 ON t1.B_PosterId = t2.U_Number<br />	WHERE t1.B_Main  = $current<br />	$Viewable<br />	ORDER BY B_Number<br />	$Limit<br />";



also the way how the number of pages is calculated is unnecessarily complicated.

this:

Code
$length = $length - 1;<br />$pages = $length / $Totaldisplay;<br />if (is_int($pages)) {<br />	$pages++;<br />}<br />$pages = ceil($pages); 


can be replaced by:

Code
$pages = ceil($length / $Totaldisplay);

Sponsored Links
Joined: Sep 2003
Posts: 488
Code Monkey
Code Monkey
Joined: Sep 2003
Posts: 488
[]Anno said:
LEFT JOIN {$config['tbprefix']}Users AS t2 ON t1.B_PosterId = t2.U_Number
WHERE t1.B_Main = $current
$Viewable
ORDER BY B_Number
$Limit
";[/code] [/]

Just curious, have you done any benchmarks?

Chances are that this will slow down the query, so there may be a tradeoff involved - especially of concern to those with large posts tables.

When you LEFT JOIN the query, evaluator can't use underlying indexes on the result table which means
the 'WHERE' and 'ORDER BY' clauses will be evaluated by a
table scan.

I tend to avoid left joins wherever possible.

The 2nd tweak is nice, I already had implemented something very similar on my site.

Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
>Just curious, have you done any benchmarks?

I did now, but only on a very small database. On the small database the new query is faster.

Do you mind running it on yours? You will have to adjust the include if you are running it on 6.4.
Attachments
124790-bench.zip (0 Bytes, 2 downloads)

Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
OK, I was able to do a benchmark test on a database with 2 millions posts.

50 threads are randomly selected and then queried from the database.
The script is at the bottom of this post.

The results for several runs:

Code
time original: 0.359662294388<br />time new:      0.36652636528<br /><br />time original: 0.355769395828<br />time new:      0.362692594528<br /><br />time original: 0.361973524094<br />time new:      0.362029075623<br /><br />time original: 0.384537220001<br />time new:      0.378733873367<br /><br />time original: 0.358870267868<br />time new:      0.355617761612<br /><br />time original: 0.41309762001<br />time new:      0.4050989151 


As you can see, both queries take equal times to execute.




The testing script:

Code
<?php<br /><br />// Require the library<br />require ("./includes/main.inc.php");<br /><br />//get the main posts numbers<br />$query = "<br />	SELECT DISTINCT B_Main<br />	FROM {$config['tbprefix']}Posts<br />	ORDER BY RAND()<br />	LIMIT 50<br />";<br /><br />$sth = $dbh -> do_query($query,__LINE__,__FILE__);<br />while (list ($current) = $dbh -> fetch_array($sth)){<br />	echo "$current, ";<br /><br />	//original query<br />	$query = "<br />		SELECT t1.B_Number,t2.U_Username,t1.B_Posted,t1.B_IP,t1.B_Subject,t1.B_Body,t1.B_File,t1.B_Status,t1.B_Approved,t2.U_Picture,t1.B_Reged,t2.U_Title,t2.U_Color,t1.B_Icon,t1.B_Poll,t1.B_Parent,t2.U_Status,t2.U_Signature,t1.B_LastEdit,t1.B_LastEditBy,t2.U_Location,t2.U_TotalPosts,t2.U_Registered,t2.U_Rating,t2.U_Rates,t2.U_RealRating,t2.U_PicWidth,t2.U_PicHeight,t2.U_Number,t1.B_FileCounter,t1.B_AnonName,t1.B_ParentUser,t2.U_Birthday,t2.U_ShowBday,t1.B_AddSig<br />		FROM  {$config['tbprefix']}Posts AS t1,<br />		{$config['tbprefix']}Users AS t2<br />		WHERE t1.B_Main  = $current<br />		AND   t1.B_PosterId = t2.U_Number<br />		ORDER BY B_Number<br />	";<br />	<br />	$time_start = getdamicrotime();<br />	$sth1 = $dbh -> do_query($query,__LINE__,__FILE__);<br />	$dbh -> finish_sth($sth1);<br />	$time_end = getdamicrotime();<br />	$time1 = $time1 + $time_end - $time_start;<br /><br /><br />	//new query<br />	$query = "<br />		SELECT t1.B_Number,t2.U_Username,t1.B_Posted,t1.B_IP,t1.B_Subject,t1.B_Body,t1.B_File,t1.B_Status,t1.B_Approved,t2.U_Picture,t1.B_Reged,t2.U_Title,t2.U_Color,t1.B_Icon,t1.B_Poll,t1.B_Parent,t2.U_Status,t2.U_Signature,t1.B_LastEdit,t1.B_LastEditBy,t2.U_Location,t2.U_TotalPosts,t2.U_Registered,t2.U_Rating,t2.U_Rates,t2.U_RealRating,t2.U_PicWidth,t2.U_PicHeight,t2.U_Number,t1.B_FileCounter,t1.B_AnonName,t1.B_ParentUser,t2.U_Birthday,t2.U_ShowBday,t1.B_AddSig<br />		FROM  {$config['tbprefix']}Posts AS t1<br />		LEFT JOIN {$config['tbprefix']}Users AS t2 ON t1.B_PosterId = t2.U_Number<br />		WHERE t1.B_Main  = $current<br />		ORDER BY B_Number<br />	";<br /><br />	$time_start = getdamicrotime();<br />	$sth1 = $dbh -> do_query($query,__LINE__,__FILE__);<br />	$dbh -> finish_sth($sth1);<br />	$time_end = getdamicrotime();<br />	$time2 = $time2 + $time_end - $time_start;<br />}<br /><br /><br />echo "<br />	<br>time original: $time1<br><br />	time new: $time2<br />";<br /><br /><br />function getdamicrotime(){<br />   list($usec, $sec) = explode(" ",microtime());<br />   return ((float)$usec + (float)$sec);<br />}<br /><br />?>  

Joined: Oct 2003
Posts: 2,305
Old Hand
Old Hand
Joined: Oct 2003
Posts: 2,305
yeah its a little slower to use the LEFT JOIN method but not astronomical.

Sponsored Links
Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
>yeah its a little slower to use the LEFT JOIN method

According to my benchmarks it is not slower.

Joined: Sep 2003
Posts: 488
Code Monkey
Code Monkey
Joined: Sep 2003
Posts: 488
Ok, that's gravy then Anno, at least you confirmed it which is the most important part.

With LEFT JOIN you never really can be sure until you run the benchmarks. It depends on the specific query, and the potential speed decrease can vary dramatically from nothing, all the way up to hundreds of times slower, depending on the table structure and what's being done.


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Gizmo
Gizmo
Portland, OR, USA
Posts: 5,833
Joined: January 2000
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)