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  (3281)
  • LimpiaDocus
    LimpiaDocus001.exe  (2550)
  • RCM
    rcm001.zip  (2186)
  • Popmail
    popmail-0.4-psmn.tar.gz  (2257)
  • Manual de Delphi en PDF
    delphi_pdf.zip  (2850)
  • Evangelio del Perl
    Evangelio_del_perl.pdf  (2560)
  • Excel Simpsons
    Excel Simpsons  (2800)
  • . . .

    ltimos comentarios

  • 358 - Anonimo: http://rs285.rapidshare.com/files/202001494/Repair_v2.9.1.1.rar aqui el link del programa
  • 358 - Anonimo: La solucion a todos los problemas de proteccion de escrituras...
  • 660 - FLORENCIA: :noworry: :roll:
  • 371 - Anonimo:
  • 707 - Anonimo: hola quiero reinstalr el SO xp con el que venia...
  • 371 - steveAnonimo: cuantos euros puedo comprar con 4000.00 dolares
  • . . .

    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  (1065), cacheando webs
  •  Spam.la  (1252), correo de usar y tirar
  •  Mailinator  (1220), correo de usar y tirar
  •  Bug me not  (1099), absurdos registros di NO
  •  Trashmail  (1126), correo de usar y tirar

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

    . . .