--
-- Podstawy indeksowania
--

CREATE TABLE People ( 
   last_name  varchar(50) not null, 
   first_name varchar(50) not null, 
   dob date not null, 
   key(last_name, first_name, dob) 
);



--
-- Strategie indeksowania w celu osiągnięcia maksymalnej wydajności
--

CREATE TABLE sakila.city_demo(city VARCHAR(50) NOT NULL); 
INSERT INTO sakila.city_demo(city) SELECT city FROM sakila.city; 
-- Repeat the next statement five times: 
INSERT INTO sakila.city_demo(city) SELECT city FROM sakila.city_demo; 
-- Now randomize the distribution (inefficiently but conveniently): 
UPDATE sakila.city_demo 
   SET city = (SELECT city FROM  sakila.city ORDER BY RAND() LIMIT 1);



SELECT COUNT(*) AS c, city 
FROM sakila.city_demo
GROUP BY city ORDER BY c DESC LIMIT 10; 



SELECT COUNT(*) AS c, LEFT(city, 3) AS pref 
FROM sakila.city_demo GROUP BY pref ORDER BY c DESC LIMIT 10; 



SELECT COUNT(*) AS c, LEFT(city, 7) AS pref 
FROM sakila.city_demo GROUP BY pref ORDER BY c DESC LIMIT 10; 



SELECT COUNT(DISTINCT LEFT(city, 3))/COUNT(*) AS sel3, 
   COUNT(DISTINCT LEFT(city, 4))/COUNT(*) AS sel4, 
   COUNT(DISTINCT LEFT(city, 5))/COUNT(*) AS sel5, 
   COUNT(DISTINCT LEFT(city, 6))/COUNT(*) AS sel6, 
   COUNT(DISTINCT LEFT(city, 7))/COUNT(*) AS sel7 
FROM sakila.city_demo; 



SELECT COUNT(*) AS c, LEFT(city, 4) AS pref 
FROM sakila.city_demo GROUP BY pref ORDER BY c DESC LIMIT 5; 



CREATE TABLE t (
     c1 INT,
     c2 INT,
     c3 INT,
     KEY(c1),
     KEY(c2),
     KEY(c3)
);



EXPLAIN SELECT film_id, actor_id FROM sakila.film_actor
WHERE actor_id = 1 OR film_id = 1\G



SELECT COUNT(DISTINCT staff_id)/COUNT(*) AS staff_id_selectivity,
COUNT(DISTINCT customer_id)/COUNT(*) AS customer_id_selectivity,
COUNT(*)
FROM payment\G



SELECT COUNT(DISTINCT threadId) AS COUNT_VALUE
FROM Message
WHERE (groupId = 10137) AND (userId = 1288826) AND (anonymous = 0)
ORDER BY priority DESC, modifiedDate DESC



SELECT COUNT(*), SUM(groupId = 10137),
SUM(userId = 1288826), SUM(anonymous = 0)
FROM Message\G



CREATE TABLE layout_test ( 
   col1 int NOT NULL, 
   col2 int NOT NULL, 
   PRIMARY KEY(col1), 
   KEY(col2) 
);



CREATE TABLE userinfo ( 
   id int unsigned NOT NULL AUTO_INCREMENT, 
   name varchar(64) NOT NULL DEFAULT '', 
   email varchar(64) NOT NULL DEFAULT '', 
   password varchar(64) NOT NULL DEFAULT '', 
   dob date DEFAULT NULL, 
   address varchar(255) NOT NULL DEFAULT '', 
   city varchar(64) NOT NULL DEFAULT '', 
   state_id tinyint unsigned NOT NULL DEFAULT '0', 
   zip varchar(8) NOT NULL DEFAULT '', 
   country_id smallint unsigned NOT NULL DEFAULT '0', 
   gender ('M','F') NOT NULL DEFAULT 'M', 
   account_type varchar(32) NOT NULL DEFAULT '', 
   verified tinyint NOT NULL DEFAULT '0', 
   allow_mail tinyint unsigned NOT NULL DEFAULT '0', 
   parrent_account int unsigned NOT NULL DEFAULT '0', 
   closest_airport varchar(3) NOT NULL DEFAULT '', 
   PRIMARY KEY (id), 
   UNIQUE KEY email (email), 
   KEY country_id (country_id), 
   KEY state_id (state_id), 
   KEY state_id_2 (state_id,city,address) 
) ENGINE=InnoDB



EXPLAIN SELECT actor_id, last_name 
FROM sakila.actor WHERE last_name = 'HOPPER'\G 



CREATE TABLE rental ( 
   ... 
   PRIMARY KEY (rental_id), 
   UNIQUE KEY rental_date (rental_date,inventory_id,customer_id), 
   KEY idx_fk_inventory_id (inventory_id), 
   KEY idx_fk_customer_id (customer_id), 
   KEY idx_fk_staff_id (staff_id), 
   ... 
);



EXPLAIN SELECT rental_id, staff_id FROM sakila.rental 
WHERE rental_date = '2005-05-25' 
ORDER BY inventory_id, customer_id\G 



EXPLAIN SELECT actor_id, title FROM sakila.film_actor 
INNER JOIN sakila.film USING(film_id) ORDER BY actor_id\G 



CREATE TABLE test ( 
   ID INT NOT NULL PRIMARY KEY, 
   A INT NOT NULL,
   B INT NOT NULL,
   UNIQUE(ID), 
   INDEX(ID) 
) ENGINE=InnoDB;



CREATE TABLE userinfo (
      id int unsigned NOT NULL AUTO_INCREMENT,
      name varchar(64) NOT NULL DEFAULT '',
      email varchar(64) NOT NULL DEFAULT '',
      password varchar(64) NOT NULL DEFAULT '',
      dob date DEFAULT NULL,
      address varchar(255) NOT NULL DEFAULT '',
      city varchar(64) NOT NULL DEFAULT '',
      state_id tinyint unsigned NOT NULL DEFAULT '0',
      zip varchar(8) NOT NULL DEFAULT '',
      country_id smallint unsigned NOT NULL DEFAULT '0',
      account_type varchar(32) NOT NULL DEFAULT '',
      verified tinyint NOT NULL DEFAULT '0',
      allow_mail tinyint unsigned NOT NULL DEFAULT '0',
      parrent_account int unsigned NOT NULL DEFAULT '0',
      closest_airport varchar(3) NOT NULL DEFAULT '',
      PRIMARY KEY (id),
      UNIQUE KEY email (email),
      KEY country_id (country_id),
      KEY state_id (state_id)
     ) ENGINE=InnoDB



ALTER TABLE userinfo DROP KEY state_id, 
ADD KEY state_id_2 (state_id, city, address);
