Reading a random row from a MySQL table with Perl DBI

Por Saiyine Enviar correo el 2010-01-25 09:21:17 - Secciones: PERL DBI PROGRAMACION ENGLISH MYSQL - Enlace permanente: 902

This is the code from one of the clients used in the post "Statistics on getting a random row from a table" to check for the quickest way to access a random row.

It does nothing but reading a thousand random rows, I hope it's enough for you to learn how to access MySQL databases using Perl and use it as a quickstart for your own scripts.

#!/usr/bin/perl

use DBI;
use DBD::mysql;

$platform = "mysql";
$database = "database";
$host = "server_ip";
$port = "3306";
$user = "user";
$pw = "12345";

$dsn = "dbi:$platform:$database:$host:$port";

$connect = DBI->connect($dsn, $user, $pw);

for ($i=0; $i<1000; $i++)
{
    $query = "SELECT COUNT(1) AS total FROM bench_myisam";
    $query_handle = $connect->prepare($query);
    $query_handle->execute();

    $query_handle->bind_columns(\$total);

    if ($query_handle->fetch())
    {
        $rand = int(rand($total));

        $query = "SELECT id FROM bench_myisam LIMIT 1 OFFSET $rand";
        $query_handle = $connect->prepare($query);
        $query_handle->execute();

        $query_handle->bind_columns(\$id);
        $query_handle->fetch();
    }  
}

Otras entradas de la web que podrian interesarte:

  • (901)  Jan 24  PRINCIPAL ENGLISH MYSQL SQL  Statistics on getting a random row from a table
  • (828)  May 28  MYSQL ENGLISH  Get the last AUTO_INCREMENT ID in MySQL
  • (850)  Nov 15  MYSQL  Optimizar querys en MySQL con Explain
  • (827)  May 28  MYSQL  Obtener el ultimo AUTO_INCREMENT
  • (830)  May 28  PERL LINUX ENGLISH  Installing CPAN Perl modules made easy

  • Tu nombre (Nick):

    Tu correo (Email):
      Necesario para tu gravatar!

    Tu página (URL):

    Escribe aqui tu comentario:

           :noworry: :roll: :huh: :push: :OO 8) 8O :( :) :? :D :P :o :x :| ;) ^^ xD



    Vista previa activada.

    Vista previa (6/6/6, 6:66)


    Fondos de pantalla

    . . .

    Descargas

  • ApagaPC
    apagapc241.exe  (3265)
  • LimpiaDocus
    LimpiaDocus001.exe  (2534)
  • RCM
    rcm001.zip  (2175)
  • Popmail
    popmail-0.4-psmn.tar.gz  (2248)
  • Manual de Delphi en PDF
    delphi_pdf.zip  (2840)
  • Evangelio del Perl
    Evangelio_del_perl.pdf  (2550)
  • Excel Simpsons
    Excel Simpsons  (2790)
  • . . .

    Proyectos Online

  • Saiyine Store
  • Kunowalls!!!
  • Fondos de pantalla
  • Picaday: imagenes sexys o chocantes.
  • Scarlett: fotos de Scarlett Johansson
  • WhatsmyIP: obtener tu IP pública
  • Uma Thurman: galeria de Uma Thurman
  • FunPics: imagenes graciosas
  • . . .

    Blogs

  • Por lo que más querais, no entreis a estos: Tapanez, Yhandros, Onez.

  • Otros en español: El Mundo Today, La libreta de Van Gaal, Chavalina, Kirai, Mundo Geek, Microsiervos.

  • Mis lecturas en guiri: Michael Yon, Coding Horror, YCombinator news, MySQL Performance, Slashdot.

    . . .
  • Utilidades

  •  Coral  (1060), cacheando webs
  •  Spam.la  (1245), correo de usar y tirar
  •  Mailinator  (1213), correo de usar y tirar
  •  Bug me not  (1094), absurdos registros di NO
  •  Trashmail  (1121), correo de usar y tirar

    . . .
  • Página web ©2001-2010 Saiyine generada en s, con 4091695 visitas en total, hoy (de un total de 0 previstas).

    . . .