Here is an interesting way to join two tables, using function! I do not know why anyone will need to use this method though.
drop table if exists hindi;
drop table if exists english;
create table hindi(id int, HindiWord varchar(100), primary key(id));
create table english(id int, EnglishWord varchar(100), primary key(id));
insert into hindi values ('1', 'ek'), ('2', 'do'), ('3', 'teen');
insert into english values ('3', 'three'), ('4', 'four'), ('5', 'five');
DROP FUNCTION IF EXISTS getEnglishName;
delimiter ;;
CREATE FUNCTION getEnglishName(EngId INT(15)) RETURNS VARCHAR(300)
BEGIN
DECLARE name VARCHAR(300) DEFAULT '';
select Englishword INTO name from english where id=EngId;
return name;
END;
;;
delimiter ;
mysql> select * from hindi;
+------+-----------+
| id | HindiWord |
+------+-----------+
| 1 | ek |
| 2 | do |
| 3 | teen |
+------+-----------+
3 rows in set (0.00 sec)
mysql> select * from english;
+------+-------------+
| id | EnglishWord |
+------+-------------+
| 3 | three |
| 4 | four |
| 5 | five |
+------+-------------+
3 rows in set (0.00 sec)
The "normal" way of joining tables is using the INNER JOIN syntax as shown below:
mysql> select a.id, a.HindiWord, b.EnglishWord from hindi as a inner join english as b on a.id = b.id;
+------+-----------+-------------+
| id | HindiWord | EnglishWord |
+------+-----------+-------------+
| 3 | teen | three |
+------+-----------+-------------+
1 row in set (0.01 sec)
mysql> select a.id, a.HindiWord, b.EnglishWord from hindi as a left join english as b on a.id = b.id;
+------+-----------+-------------+
| id | HindiWord | EnglishWord |
+------+-----------+-------------+
| 1 | ek | NULL |
| 2 | do | NULL |
| 3 | teen | three |
+------+-----------+-------------+
3 rows in set (0.00 sec)
mysql> select a.id, a.HindiWord, b.EnglishWord from hindi as a left join english as b on a.id = b.id where b.id is null;
+------+-----------+-------------+
| id | HindiWord | EnglishWord |
+------+-----------+-------------+
| 1 | ek | NULL |
| 2 | do | NULL |
+------+-----------+-------------+
2 rows in set (0.00 sec)
_____
You can achive the same results of Inner, Left and Left Outer joins using function. The only difference is that the "NULL" value is not getting generated.
mysql> select a.id, a.HindiWord, getEnglishName(a.id) from hindi as a where getEnglishName(a.id) != '';
+------+-----------+----------------------+
| id | HindiWord | getEnglishName(a.id) |
+------+-----------+----------------------+
| 3 | teen | three |
+------+-----------+----------------------+
1 row in set, 2 warnings (0.00 sec)
mysql> select a.id, a.HindiWord, getEnglishName(a.id) from hindi as a;
+------+-----------+----------------------+
| id | HindiWord | getEnglishName(a.id) |
+------+-----------+----------------------+
| 1 | ek | |
| 2 | do | |
| 3 | teen | three |
+------+-----------+----------------------+
3 rows in set, 2 warnings (0.01 sec)
mysql> select a.id, a.HindiWord, getEnglishName(a.id) from hindi as a where getEnglishName(a.id) = '';
+------+-----------+----------------------+
| id | HindiWord | getEnglishName(a.id) |
+------+-----------+----------------------+
| 1 | ek | |
| 2 | do | |
+------+-----------+----------------------+
2 rows in set, 4 warnings (0.00 sec)
The query using function will not use indexes and hence the query will be slower than the standard join. But it is interesting to know different ways of joining data. I had a chance to see PHP code that used a loop to take one record from a table and join with the other. and yes, that too was inefficient way of joining tables.
Labels: mysql tips
There are times when you need to stop logging the general queries as well as slow and binary queries. For e.g. when I am restoring data from a dump file, I do not want all the queries to enter in the log files for obvious reasons.
Here is the simple shell script that will enable and disable the logs as and when required.
$ cat disable_logs.sh
#/bin/sh
# disable logs by adding the comment
# backup the current my.cnf first
cp /etc/my.cnf /home/my_$(date +'%d-%m-%y-%H-%M').cnf
sed -i "s|^log-slow-queries=.*$|#\\0|" /etc/my.cnf
sed -i "s|^log-bin=.*$|#\\0|" /etc/my.cnf
sed -i "s|^log=.*$|#\\0|" /etc/my.cnf
$ cat enable_logs.sh
#/bin/sh
# enable logs by removing the comment
# backup the current my.cnf first
cp /etc/my.cnf /home/my_$(date +'%d-%m-%y-%H-%M').cnf
sed -i "s/^#log-slow-queries=/log-slow-queries=/" /etc/my.cnf
sed -i "s/^#log-bin=/log-bin=/" /etc/my.cnf
sed -i "s/^#log=/log=/" /etc/my.cnf
Restart the MySQL service after running this script.
Labels: mysql tips, shell script
Every table in your database should have a primary key. A table without the primary key is like an orphan child without the support of parents or a youth without a spouse. You never know what you are missing!
1) You can easily check if the table has a primary key or not.
show create table tblName;
2) Once you have found that there is no primary key, you must check if a column or a group of columns can be declared as a primary key.
ALTER TABLE `tblName` ADD PRIMARY KEY ( `myID` , `yourID` , `type` )
3) If there is no such candidate column or a group of columns that is Unique and not null, you can simply add an auto-incremented column as shown below.
ALTER TABLE `tblName` add column autoID int not null auto_increment, add primary key (autoID)
4) If you want to remove the newly added column autoID, you can simply use the following statement.
ALTER TABLE `tblName` drop primary key, drop column autoID
This simple task can dramatically improve the performance and will make it compliant without affecting the application in any way.
Use the following shell script to find out the candidate column that can be declared as Primary.
# cat primary.sh
#!/bin/sh
# the script to find out the tables those do not have a primary key
# It will show the max and min values stored in each column along with the create table output
# change the dbname to the database name that you want to analyse
dbname='shantanu'
> /home/develop/tosave.txt
while read mytable
do
mysql shantanu -Bse"show create table $mytable\G" >> /home/develop/tosave.txt
mysql shantanu -Bse"select * from $mytable procedure analyse()\G" >> /home/develop/tosave.txt
done << heredoc
`mysql -Bse"select t.table_name as tbl from INFORMATION_SCHEMA.TABLES AS t LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c ON (t.TABLE_NAME=c.TABLE_NAME AND c.CONSTRAINT_SCHEMA=t.TABLE_SCHEMA AND constraint_name='PRIMARY') WHERE t.table_schema='$dbname' AND constraint_name IS NULL;"`
heredoc
_____
== need for Primary Key ==
It is interesting to know what happens if we do not have a primary key to uniquely differnetiate the rows.
Here is an example table.
mysql> select * from hari;
+------+------------+-------+
| id | mydate | email |
+------+------------+-------+
| 1 | 2009-10-10 | 1111 |
| 1 | 2009-10-10 | 1111 |
| 1 | 2009-10-10 | 2222 |
| 2 | 2010-11-11 | 3333 |
| 2 | 2010-11-11 | 3333 |
+------+------------+-------+
5 rows in set (0.00 sec)
The problem with the above table is that the first 2 and last 2 rows are actually duplicate and there is no way to identify them since there is no unique or primary key used in the table.
Here is the SQL code that you can run to create this test table.
drop table if exists hari;
CREATE TABLE `hari` (
`id` int(11) default NULL,
`mydate` date default NULL,
`email` varchar(100) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `hari` VALUES (1,'2009-10-10','1111'),(1,'2009-10-10','1111'),(1,'2009-10-10','2222'),(2,'2010-11-11','3333'),(2,'2010-11-11','3333');
In order to get the count of the unique ID's we need the following query.
SELECT
count(sel2.refid2 ) as recCount,
refid2, recDate2
FROM
(SELECT id AS refid2, mydate AS recDate2
FROM hari AS ol_Email1
GROUP BY ol_Email1.id, ol_Email1.mydate, ol_Email1.email
) AS sel2
GROUP BY sel2.refid2;
+----------+--------+------------+
| recCount | refid2 | recDate2 |
+----------+--------+------------+
| 2 | 1 | 2009-10-10 |
| 1 | 2 | 2010-11-11 |
+----------+--------+------------+
2 rows in set (0.00 sec)
The query shown above is ugly, confusing and slow since the indexes will not be used properly. So I add a primary key to the table.
ALTER TABLE hari ADD COLUMN pid INT NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (pid);
I get the same results as shown above, but with much cleaner query that is also using the primary key.
select count(*) as ercCount, a.id as refid2, a.mydate as recDate2 from hari as a inner join
(SELECT pid, id, mydate FROM hari GROUP BY id, mydate, email) as dt
on a.pid = dt.pid group by a.id;
+----------+--------+------------+
| ercCount | refid2 | recDate2 |
+----------+--------+------------+
| 2 | 1 | 2009-10-10 |
| 1 | 2 | 2010-11-11 |
+----------+--------+------------+
2 rows in set (0.00 sec)
Labels: mysql tips, shell script