PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > PreparedStatements with variable number of parameters
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
PreparedStatements with variable number of parameters

Réponse
 
LinkBack Outils de la discussion
Vieux 17/07/2006, 21h40   #1
bennett.matthew@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut PreparedStatements with variable number of parameters

Hello,

Is there a good way to make PreparedStatements with a variable number
of parameters?

My queries look something like this:

SET @rowid := null;
SELECT * FROM (
SELECT
id,
name,
@rowid := COALESCE(@rowid + 1, 1) AS rowid
FROM
mytable
ORDER BY
name
) AS myquery
WHERE rowid IN (5, 7, 12, 20);

And I can have a variable number of IN terms.

At the moment, I'm building the PreparedStatement with a concatenated
String.

I've seen another discussion of this topic online, and the final
implementation used there was to build a cache of PreparedStatements,
so you would (hopefully) only have to concatenate the String once. But
I have so many different combinations it's unlikely the same query will
come up twice in quick succession.

Is this really the best way of doing things. Indeed, is there any
(beyond security) benefit of using PreparedStatements in this case?

Thanks,
Matt.

  Réponse avec citation
Vieux 17/07/2006, 22h18   #2
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PreparedStatements with variable number of parameters

bennett.matthew@gmail.com wrote:
> Is there a good way to make PreparedStatements with a variable number
> of parameters?


No, there is no way to put a variable number of values into the IN
clause by using statement parameters. Statement parameters can
substitute only a single value; not a list of values, or any other
syntactic element.

If you can predict a finite limit to the number of values you will want
to compare against, you could do this:

WHERE rowid IN (?, ?, ?, ?, ?, ?, ?, ? ... )

That is, list as many parameters as the greatest number of distinct
values you could list in the IN predicate. When you plug values into
the parameters, you can either supply NULLs for the parameters you don't
need, because rowid IN (1,2,3,NULL) is the same as rowid IN (1,2,3). Or
else you can repeat your list of values, because rowid IN (1,2,3,1,2,3)
is the same as rowid IN (1,2,3).

> At the moment, I'm building the PreparedStatement with a concatenated
> String.


I recommend that you continue to build the prepared statement with a
concatenated string.

Regards,
Bill K.
  Réponse avec citation
Vieux 18/07/2006, 00h00   #3
bennett.matthew@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PreparedStatements with variable number of parameters

Hello Bill,

Thanks for the advice.

> If you can predict a finite limit to the number of values you will want
> to compare against, you could do this:
>
> WHERE rowid IN (?, ?, ?, ?, ?, ?, ?, ? ... )


I can predict a maximum number of requests, but it's about 100. What
would be the performance hit of trying to find ~100 results that aren't
there?

Regards,
Matt.

  Réponse avec citation
Vieux 18/07/2006, 05h50   #4
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PreparedStatements with variable number of parameters

bennett.matthew@gmail.com wrote:
> I can predict a maximum number of requests, but it's about 100. What
> would be the performance hit of trying to find ~100 results that aren't
> there?


I don't know the answer to that for certain, so you should try a couple
of experiments using your database, and time the result to see if
there's any significant difference between IN compared with 3 constant
values vs. 100 constant values. A test using your database would be a
more relevant answer than a supposition about the performance. But my
guess is that the difference will be negligible.

I should alter my recommendation about using NULL parameters, based on
this paragraph I just read:

"To comply with the SQL standard, IN returns NULL not only if the
expression on the left hand side is NULL, but also if no match is found
in the list and one of the expressions in the list is NULL."
http://dev.mysql.com/doc/refman/5.0/...operators.html

So "WHERE rowid IN (1, 2, 3, NULL)" _doesn't_ do the same thing as
"WHERE rowid IN (1,2,3)".

Regards,
Bill K.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09164 seconds with 12 queries