1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `users`
--------------
--------------
CREATE TABLE `users`
(
`id` int NOT NULL AUTO_INCREMENT,
`login` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `idx_login` (`login`,`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `users` (`login`) values
('user01'),('user02')
--------------
--------------
select * from users
--------------
+----+--------+
| id | login |
+----+--------+
| 1 | user01 |
| 2 | user02 |
+----+--------+
--------------
DROP TABLE IF EXISTS `session_ipv4`
--------------
--------------
CREATE TABLE `session_ipv4`
(
`id` bigint NOT NULL AUTO_INCREMENT,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`user_login_id` int NOT NULL,
`ip` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FOREIGN` FOREIGN KEY (`user_login_id`) REFERENCES `users` (`id`) ON DELETE cascade ON UPDATE cascade,
INDEX `idx_end_date` (`user_login_id`,`end_date`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `session_ipv4` (`start_date`,`end_date`,`user_login_id`,`ip`) values
('2014-04-01', '2014-07-14', 1, '192.168.1.33'),
('2015-03-01', '2015-12-31', 1, '192.168.1.33'),
('2015-01-01', '2015-02-07', 2, '192.168.1.25'),
('2015-03-01', '2015-03-31', 2, '192.168.1.25')
--------------
--------------
select * from session_ipv4
--------------
+----+------------+------------+---------------+--------------+
| id | start_date | end_date | user_login_id | ip |
+----+------------+------------+---------------+--------------+
| 1 | 2014-04-01 | 2014-07-14 | 1 | 192.168.1.33 |
| 2 | 2015-03-01 | 2015-12-31 | 1 | 192.168.1.33 |
| 3 | 2015-01-01 | 2015-02-07 | 2 | 192.168.1.25 |
| 4 | 2015-03-01 | 2015-03-31 | 2 | 192.168.1.25 |
+----+------------+------------+---------------+--------------+
--------------
explain
SELECT COUNT(*)
FROM session_IPV4 s
INNER JOIN users u
ON s.user_login_id = u.id
WHERE (u.login like 'user02')
and (end_date between '2015-01-01' and '2015-04-01')
--------------
+----+-------------+-------+------------+-------+-------------------+--------------+---------+-----------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+-------------------+--------------+---------+-----------+------+----------+--------------------------+
| 1 | SIMPLE | u | NULL | range | PRIMARY,idx_login | idx_login | 257 | NULL | 1 | 100.00 | Using where; Using index |
| 1 | SIMPLE | s | NULL | ref | idx_end_date | idx_end_date | 4 | base.u.id | 1 | 25.00 | Using where; Using index |
+----+-------------+-------+------------+-------+-------------------+--------------+---------+-----------+------+----------+--------------------------+
--------------
set @id=0
--------------
--------------
explain
select id into @id
from users
where login like 'user02'
--------------
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | users | NULL | range | idx_login | idx_login | 257 | NULL | 1 | 100.00 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+--------------------------+
--------------
explain
SELECT COUNT(*)
FROM session_IPV4
WHERE (user_login_id = @id)
and (end_date between '2015-01-01' and '2015-04-01')
--------------
+----+-------------+--------------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | session_IPV4 | NULL | range | idx_end_date | idx_end_date | 7 | NULL | 1 | 100.00 | Using where; Using index |
+----+-------------+--------------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager