vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   mysql queires and UNION?? (https://vborg.vbsupport.ru/showthread.php?t=171026)

grant.hayman 02-20-2008 03:15 PM

mysql queires and UNION??
 
I have an advertsers database which baisicly holds banner code and some parameters to distinguish placement and usergroups etc.

I have have 4 ad slots which very in size, shape and how many banners it will hold etc.

So to call the banner code i need 4 queries, one for each slot. Which means 4 db hits.

If i write the query as a UNION query and order the result as i need will this be a lot better for vbulletin for overhead etc or since it is doing 4 selects anyway does it really make much difference.

Baisicly do UNION queries help with vbulletin overhead is it classed as 1 hit to the db instead of 4.

Thanks

Grant

MoT3rror 02-20-2008 10:24 PM

If you have your ads in one table in your database, you should be able to just do one query.

grant.hayman 02-21-2008 06:59 AM

hi MoT3rror,

I can't see a way of doing this in 1 query as the 4 selects are getting diffrent type of ads, size and position plus each select will be getting random results.

from what I can see it would definetly need 4 selects but was wondering if union them all would be better for vb so that all the results will come back in 1 result set.

it would be easier to do 4 diffrent selects from a coding point of view but I am thinking it might be a bit heavy on vb to do 4 extra selects.

simple example:

Code:

Select bannercode from table where "where clause"
ORDER BY RAND() LIMIT 1;
UNION
Select bannercode from table where "another where clause"
ORDER BY RAND() LIMIT 2;
UNION
Select bannercode from table where "another where clause"
ORDER BY RAND() LIMIT 1;
UNION
Select bannercode from table where "another where clause"
ORDER BY RAND() LIMIT 1;

Grant

----------------------------------------

After playin with this it seems it is impossible to union and rand() all the selects so it looks like im going to have to do 4 selects anyway :(

MoT3rror 02-21-2008 12:14 PM

Have you try doing a limit of 4 on your query like this?
[SQL]SELECT bannercode FROM table WHERE ... ORDER BY RAND() LIMIT 4[/SQL]

grant.hayman 02-21-2008 01:49 PM

Quote:

Originally Posted by MoT3rror (Post 1448069)
Have you try doing a limit of 4 on your query like this?
[SQL]SELECT bannercode FROM table WHERE ... ORDER BY RAND() LIMIT 4[/SQL]


But that would get 4 results per select I thought....

I will give it a go.

Amenadiel 02-25-2008 02:08 AM

I believe that using union will cause even more database load than making the queries on a separate basis. This is because the union instruction does an implicit distinct which in this case you just don't need.

You could use "union all" instead but, anyway, this would end up using the same database resources than the 4 queries separately.


All times are GMT. The time now is 10:26 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01122 seconds
  • Memory Usage 1,721KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete