From: niklas | brueckenschlaeger Date: 2010-11-15T18:16:40+09:00 Subject: Re: Mysql::Result .each_hash - unexpected result --=-d9S2JR2pUWxZhhy6CacP Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, 2010-11-15 at 18:05 +0900, Andy Tolle wrote: > > One way of solving this would be to keep the results you want to re-use > > within an array: > > > > Hope this helps. >=20 > Niklas, >=20 > I'm not too fond using multiple variables to store one thing unless=20 > there is a good reson for it... so I'm wondering: if I question the=20 > Mysql::Result-set, does it then query the database each time I iterate=20 > over his set? No, the database is not queried every time. The reason I was suggesting to use an array is that it that that way you don't need to handle that specially. It works like any other ruby array, unlike the Mysql::Result, which you need to reset before iterating again. So if you're about to iterate over the result multiple times, possibly in different places it's save to either a) make your custom iterator, rewinding the result set afterwards=20 OR b) use a standard ruby type, such as Array a) might look like this (ugly monkey patch): class Mysql::Result def my_each_hash(&block) each_hash(&block) ; data_seek(0) end end Be warned though: this might give you trouble in a threaded environment. >=20 > Asked differently: you happen to know if storing the resultset in an=20 > array in stead of questioning the resultset over and over, does this=20 > have a positive influence on serverloads? >=20 > Is there any way I can find out myself when exactly the database is=20 > querried? Seems kinda crucial to optimizing. >=20 --=-d9S2JR2pUWxZhhy6CacP Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iQIcBAABAgAGBQJM4PpvAAoJEIQdQGZOjo8255sP/1WjkbJrkaDV7Z+1G6ylLk5k WzCEUxItSQMXi2pr/EmpL8fYHoFl9gMyjQDStx7/7xizD6Jqh5XEc6Skb93C+HZb b/oLb1KV8Dfyuz48Ei4U0119rjF2uxsBmNRKnxuLDQnUNqGdLPPGe+Swrjy80oo0 CkoV82wonvmZPB45o9An/tEGvu9YtxqfMfFZuhL3/6IO2nmzEXdxcP8a0IKPeMhz r25uiUBfIXwceMX+K6JD00yhZtSdR1w+IFGe4V7Z4kUuKtt6QzuAFp/FfC+SAMwa q5+pz+5VcfWnrmFJQixyucwdp3+TpyaZaMMtNiaJxnTEv3LRzha3d1zSLBUmyLPm 9jr1gRpeE0vMi7fw+jlYKIetSmUB+dXiNvrQUOlxq9QXlXu93Tgr3QQe/c5AUHcP LBRp61UkiLab8+Qm1vxAZFSWkO2sBP+6JPD9eCaVd81bbEIL3TdRyzLBdBLErluB +9huOO5ZiXheF8nmJB0niJSeh67S+5JWIPnVO2iCOeEkEYsRKL3o5w2u4pXFG8oh qrJG/oHYiNSiwbIXifFM+W+PSGR1G+0kPkUHMF/82zYsOFPN/wZsj66scOQ50Y/U y5hBlRjouyyUn8Y8jKF2kWE+nQQ78GDy0QLQdtNcFrnubpO5Cg1Qrpup5fi0t8oS m9V0rm3azLaN4Sv79HCk =ZBgi -----END PGP SIGNATURE----- --=-d9S2JR2pUWxZhhy6CacP--