Author Topic: "The" MySQL Sticky  (Read 69422 times)

0 Members and 1 Guest are viewing this topic.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
"The" MySQL Sticky
« on: February 01, 2007, 05:37:05 PM »
http://www.sqlzoo.net/

An oldie but a goodie... site is always a little too slow, but the fact that they make you write queries you can test is a definite plus for anyone who isn't versed in SQL statement syntax.
« Last Edit: December 18, 2009, 08:27:54 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
MySQL Cheat Sheet
« Reply #1 on: February 06, 2007, 08:56:55 AM »
A fantastic reference card-style sheet can be found here; available as a PDF or a PNG.

Visibone now has one too!
« Last Edit: July 07, 2009, 04:16:05 PM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
An ORDER BY RAND() alternative
« Reply #2 on: February 06, 2007, 01:25:14 PM »
So, for all of us who are used to "ORDER BY RAND() LIMIT 1" -- which doesn't scale well at all -- here's a "better" way.

With a JOIN (concept from Jan Kneschke): 
Code: [Select]
SELECT
*
FROM
tablename AS t1
INNER JOIN
(
    SELECT
    ROUND(RAND() * (SELECT MAX(id) FROM tablename)) AS id
) AS t2
ON
t1.id >= t2.id
ORDER BY
t1.id ASC
LIMIT 1;

Or with subqueries:
Code: [Select]
SELECT * FROM tablename
WHERE id >= FLOOR( RAND( ) * ( SELECT MAX( id ) FROM tablename ) )
ORDER BY id ASC
LIMIT 1

In principle, this can be extended to N rows... just remember that it's possible than <N rows are retrieved, depending on the distribution of id values -- so if it really matters that you get N back, you may want to ask for some more, just in case.  Either way, you'll still have to check the size of the result set.

Hope that helps.
« Last Edit: February 21, 2007, 10:28:23 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
MySQL Performance / Optimizing Queries
« Reply #3 on: February 08, 2007, 01:23:42 PM »
This page contains some in-depth, extremely well-written articles on query optimization in very common situations.

A collection of fantastic resources on the subject of MySQL Performance:
 
« Last Edit: February 15, 2009, 09:04:45 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
Introduction to Database Normalization / Sample Databases
« Reply #4 on: February 08, 2007, 01:25:50 PM »
MySQL's tech resource article on database normal forms is an excellent read.

NEW!!! - A large (160MB, 4M record) sample database with test suite for MySQL -- fantastic for running "real" queries.

In addition, MySQL provides a number of sample databases for testing purposes; personally, I find this one to be the most useful for playing around with SQL statements and such.

The Zip Code Database Project exists to provide US Zip Codes in their entirety; latitude and longitude coordinates included! The downloads are in CSV and MySQL table dump formats.

Also, there's an pretty good online "data generator", and SQL is one of the options... although personally, I prefer the integers table approach (courtesy of Baron Schwartz):

Code: [Select]
set @num_gamers    := 10000,
    @num_countries := 5,
    @num_games     := 10;

drop table if exists gamer;
drop table if exists game;
drop table if exists country;
drop table if exists score;
drop table if exists semaphore;

create table gamer(
   gamer int not null,
   country int not null,
   name varchar(20) not null,
   primary key(gamer)
);

create table game(
   game int not null,
   name varchar(20) not null,
   primary key(game)
);

create table score(
   gamer int not null,
   game int not null,
   score int not null,
   primary key(gamer, game),
   index(game, score),
   index(score)
);

create table country(
   country int not null,
   name varchar(20) not null,
   primary key(country)
);

-- I use the integers table to generate large result sets.
drop table if exists integers;
create table integers(i int not null primary key);
insert into integers(i) values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

insert into country(country, name)
   select t.i * 10 + u.i, concat('country', t.i * 10 + u.i)
   from integers as u
      cross join integers as t
   where t.i * 10 + u.i < @num_countries;

insert into game(game, name)
   select t.i * 10 + u.i, concat('game', t.i * 10 + u.i)
   from integers as u
      cross join integers as t
   where t.i * 10 + u.i < @num_games;

insert into gamer(gamer, name, country)
   select th.i * 1000 + h.i * 100 + t.i * 10 + u.i,
      concat('gamer', th.i * 1000 + h.i * 100 + t.i * 10 + u.i),
      floor(rand() * @num_countries)
   from integers as u
      cross join integers as t
      cross join integers as h
      cross join integers as th;

insert into score(gamer, game, score)
   select gamer.gamer, game.game, floor(rand() * @num_gamers * 10)
   from gamer
      cross join game;
« Last Edit: July 30, 2008, 11:21:21 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
MySQL Function of the Day
« Reply #5 on: March 11, 2008, 10:47:45 AM »
Basically, this blog contains a regularly-updated run-down of MySQL functions, with a short description and a few hints on how to use them; RSS feed here.
« Last Edit: March 11, 2008, 10:54:08 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
MySQL Articles, Tips & Snippets
« Reply #6 on: April 21, 2008, 02:30:12 PM »
This resource covers a very broad range of topics... it's worth a look, though, especially if you're stumbling in the early stages of configuration.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
EXPLAIN cheatsheets
« Reply #7 on: April 22, 2008, 03:27:28 PM »
At the 2008 MySQL Conference and Expo, The Pythian Group gave away EXPLAIN cheatsheets (PDF).
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,150
  • Grumpy Old Man
    • View Profile
MySQL Performance / Optimizing Queries
« Reply #8 on: June 27, 2008, 05:26:38 PM »
« Last Edit: December 18, 2009, 08:21:16 AM by fenway »

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
SQL Anti-Patterns (NEW)
« Reply #9 on: August 07, 2008, 11:54:01 AM »
Hopefully this presentation will stay online at scribd...  it's simply fantastic, probably the best I've come across in recent memory.  At 220 slides, it's quite lengthy -- but the lessons learned are invaluable, so be sure to read all the way to the end.

A MUST READ!!!!

EDIT: This year's version of the presentation -- some really great stuff in here, particuarly about hierarchies.
« Last Edit: April 21, 2009, 10:19:49 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline 448191

  • Staff Alumni
  • Fanatic
  • *
  • Posts: 3,506
  • Gender: Male
    • View Profile
Re: SQL Anti-Patterns
« Reply #10 on: August 11, 2008, 08:38:27 AM »
Apparently these slides are from a presentation at a MySQL conference:

http://en.oreilly.com/mysql2008/public/schedule/detail/1639

I have removed the uploaded PDF from my site, because the O' Reilly site doesn't specify any type of license.
« Last Edit: August 11, 2008, 08:43:36 AM by 448191 »

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
MySQL’s character sets and collations demystified
« Reply #11 on: December 08, 2008, 06:51:36 AM »
http://code.openark.org/blog/?p=10

Really nice blog entry about all these charsets, collations and how to use them (and what to take care about)
Could be added to sticky above.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline fenwayTopic starter

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,439
  • Gender: Male
    • View Profile
Developing MySQL Database Applications With PHP
« Reply #12 on: April 24, 2009, 09:54:13 AM »
Quote
A four part tutorial that explains the PHP / MySQL extensions - mysql, mysqli, and pdo_mysql - with simple examples is now accessible from Sun Developer Network.

Application developers who are new to the development of MySQL database applications with PHP are the target audience of this tutorial.

Covers:
  • Part 1: Using the MySQL Improved Extension, mysqli
  • Part 2: Using the MySQL Extension, mysql
  • Part 3: Using the PDO Extension With MySQL Driver, pdo_mysql
  • Part 4: Using the MySQL Native Driver for PHP, mysqlnd

Tutorial is here.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,815
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: An ORDER BY RAND() alternative
« Reply #13 on: May 05, 2009, 03:21:00 PM »