Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 03-28-2007, 04:04 PM
manola manola is offline
 
Join Date: Feb 2005
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default sql error

Hi!

Code:
$querya = $vbulletin->db->query_read("
		SELECT post.postid, post.pagetext, post.userid, post.username, thread.forumid, thread.title as titulo, thread.threadid, forum.forumid, forum.title
		FROM " . TABLE_PREFIX . "post as post
		LEFT JOIN " . TABLE_PREFIX . "thread as thread on (post.threadid = thread.threadid)
		LEFT JOIN " . TABLE_PREFIX . "forum as forum on (thread.forumid = forum.forumid)
		WHERE forum.forumid not in ($excluded)
		ORDER BY postid DESC
		LIMIT $limit"
	);
I got this table
POST 4 - THREAD 1 - USERX
POST 3 - THREAD 2 - USERX
POST 2 - THREAD 1 - USERX
POST 1 - THREAD 3 - USERX

I only want 1 post from every thread, in this example last 4 posts, 2 from thread 1 and other 2 from thread 2 and 3. If I only want to show 1 post from thread 1.. What i have to make?

Thanks!
Reply With Quote
  #2  
Old 03-29-2007, 06:11 PM
bluesoul's Avatar
bluesoul bluesoul is offline
 
Join Date: Jul 2004
Location: Paducah, KY USA
Posts: 60
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The problem is the normal way of fixing this, using a DISTINCT statement, is ambiguous because, well, which row of the repeating rows will it return? It doesn't seem to have that kind of logic checking. However you can sort of hack it into submission like so.

[sql]$querya = $vbulletin->db->query_read("
SELECT MAX(post.postid), post.pagetext, post.userid, post.username, thread.forumid, thread.title AS titulo, thread.threadid, forum.forumid, forum.title
FROM " . TABLE_PREFIX . "post AS post
LEFT JOIN " . TABLE_PREFIX . "thread AS thread ON (post.threadid = thread.threadid)
LEFT JOIN " . TABLE_PREFIX . "forum AS forum ON (thread.forumid = forum.forumid)
WHERE forum.forumid NOT IN ($excluded)
GROUP BY thread.threadid
ORDER BY postid DESC
LIMIT $limit"
);[/sql]
Reply With Quote
  #3  
Old 03-29-2007, 09:44 PM
manola manola is offline
 
Join Date: Feb 2005
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks bluesoul for your answer,

At end I found the correct SQL, I only need to use WHERE post.parentid = thread.lastpostid to show only 1 link for 1 thread.

Thanks, u can view the result in www.archivostv.com
Reply With Quote
  #4  
Old 03-30-2007, 05:22 AM
TECK's Avatar
TECK TECK is offline
 
Join Date: Nov 2001
Location: Canada
Posts: 4,182
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

manola, you realize how intense is your query to the server, right?
You are doing a huge scan (very bad), even if you stick to indexes only. If you have 1000 members browsing that page into a large forum, it will kill the server instantly, with average loads going over 30-40 easy. Especially if you run Apache (who is known to eat memory like an elephant).
Always use a dateline, when you play with thread or post tables. Give the smallest value possible to it, then you will be ok.
Reply With Quote
  #5  
Old 03-30-2007, 08:45 AM
manola manola is offline
 
Join Date: Feb 2005
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi teck! I dont use the query at the first post but my query is too similar.

dateline is the value int(10) of each post, how can i give it a smallest value?

Sorry, I dont understand you
Reply With Quote
  #6  
Old 03-30-2007, 04:41 PM
TECK's Avatar
TECK TECK is offline
 
Join Date: Nov 2001
Location: Canada
Posts: 4,182
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Run this into your query analyzer:
[sql]SELECT threadid
FROM thread
WHERE postusername = 'John'
AND dateline > (UNIX_TIMESTAMP(NOW()) - 172800)
AND visible = 1
-- you should really add perms here
ORDER BY lastpostid, threadid DESC
LIMIT 15;[/sql]
This query will scan the table thread for any activity done last 2 days only. It will not scan anything prior to that date event, resulting in a lot less memory usage, load to the server, etc.
In other words, you will not scan the hole table, then drop all results and keep only 15.

When I ment the smallest value, I ment this: 172800 (60 * 60 * 24 * 2)
If you have a lot of threads posted evey day, you might need to set this value 120 (1 hour).
Since you only need few threads to be displayed, experiment until you get the best time window.

Edit: You should not rely only on the dateline cut. Make sure you add another condition that will cut down even further the scanning. (like I did with username John)
Reply With Quote
  #7  
Old 03-30-2007, 08:01 PM
manola manola is offline
 
Join Date: Feb 2005
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If u want I make a statue to you.. jejeje (sorry for my english)

Very Thanks for ur advice!

I only made about 150-300 posts/day with about 340.000 posts so my forum isnt big.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:43 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03869 seconds
  • Memory Usage 2,217KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete