vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Is there a way to set data in multiple records in one query? (https://vborg.vbsupport.ru/showthread.php?t=73760)

WetWired 01-04-2005 11:38 AM

Is there a way to set data in multiple records in one query?
 
In MySQL, is there an efficient way to set a field to a specified list of values in a corresponding list of where conditions (based on the primary key), or do I have to do an individual UPDATE query for each record?

Dean C 01-04-2005 11:52 AM

Sorry you've totally lost me. Can you perhaps give an example of what you're trying to do :)?

rake 01-04-2005 01:34 PM

you could write a function to do that, but you'd still end up with more queries.

$list[0] = "...";
$list[0] = "...";

$where[0] = "...";
$where[1] = "...";

for($i = 0; $i < sizeof($list); $i++)
{
mysql_query("UPDATE table SET $list[$i] WHERE $where[$i]");
}

WetWired 01-04-2005 02:13 PM

Quote:

Originally Posted by rake
you could write a function to do that, but you'd still end up with more queries.

$list[0] = "...";
$list[0] = "...";

$where[0] = "...";
$where[1] = "...";

for($i = 0; $i < sizeof($list); $i++)
{
mysql_query("UPDATE table SET $list[$i] WHERE $where[$i]");
}

Hmm. Is it more efficient (or even supported) to send multiple queries at the same time, separated by semicolons?

Marco van Herwaarden 01-04-2005 02:22 PM

With this loop you ARE sending multiple queries.

Better to do like Dean asked and give an examplke of what you try to do.

rake 01-04-2005 02:31 PM

Quote:

Originally Posted by MarcoH64
With this loop you ARE sending multiple queries.

Better to do like Dean asked and give an examplke of what you try to do.

there's no way around sending multiple queries, if i understand correctly what wetwired wants to do.


Quote:

Hmm. Is it more efficient (or even supported) to send multiple queries at the same time, separated by semicolons?
If you can send multiple queries in one go (never tried it), it would be more efficient than what i wrote, since you wouldn't lose time looping through that for statement.

WetWired 01-04-2005 03:22 PM

Quote:

Originally Posted by rake
there's no way around sending multiple queries, if i understand correctly what wetwired wants to do.




If you can send multiple queries in one go (never tried it), it would be more efficient than what i wrote, since you wouldn't lose time looping through that for statement.

What I mean is that I would loop and accumulate the UPDATE queries in a string separated by semicolons, then send them all in the same mysql_query call. Does this do anything to minimize overhead?

Also, people are always talking about the cost of a hack in terms of queries, but I can easily construct a query that will take 10 times longer to execute than another. What gives? I hardly believe that the majority of users concerned with such things aren't using mysql as a library, so I don't understand why the number of queries should be more important than the time spent executing them.

Dean C 01-04-2005 03:32 PM

Hmmm, well by the sounds of things what you're doing doesn't sound very efficient either way. As I said before why not tell us exactly what you're trying to do then we can give tips. It's hard to offer advice without an example :)

rake 01-04-2005 03:44 PM

Quote:

Originally Posted by WetWired
What I mean is that I would loop and accumulate the UPDATE queries in a string separated by semicolons, then send them all in the same mysql_query call. Does this do anything to minimize overhead?

I know that's what you meant. But i doubt it will have any impact.

Quote:


Also, people are always talking about the cost of a hack in terms of queries, but I can easily construct a query that will take 10 times longer to execute than another. What gives? I hardly believe that the majority of users concerned with such things aren't using mysql as a library, so I don't understand why the number of queries should be more important than the time spent executing them.
EXACTLY. I'd rather have 10 fast queries than one big slow one. People generally worry too much about the query count.

filburt1 01-04-2005 04:00 PM

Quote:

Originally Posted by WetWired
In MySQL, is there an efficient way to set a field to a specified list of values in a corresponding list of where conditions (based on the primary key), or do I have to do an individual UPDATE query for each record?

Look at REPLACE INTO syntax for updating if a primary key exists, inserting otherwise.


All times are GMT. The time now is 05:02 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.01297 seconds
  • Memory Usage 1,738KB
  • 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
  • (7)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete