MySql « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ MySql ’

Alphanumeric Sorting using Criteria Query (with MySQL database)

Posted by Gaurav Sharma on February 2nd, 2012

I am working on a Grails application with MySQL database. I had a use case in which I had to implement alphanumeric sorting using Criteria Query on Grails. By alphanumeric sorting I mean if there is a class Employee with field empId then on doing :

Employee e1 = new Employee(empId:'emp10').save()
Employee e2 = new Employee(empId:'emp2').save()
Employee e3 = new Employee(empId:'emp1').save()
Employee e4 = new Employee(empId:'1emp').save()
Employee e5 = new Employee(empId:'10emp').save()
Employee e6 = new Employee(empId:'2emp').save()

Employee.createCriteria().list([sort:'empId'])
            /* OR */
Employee.createCriteria().list{
    order('empId')
}

should give result like [e4,e6,e5,e3,e2,e1]. But according to sql sorting the result would be [e5,e4,e6,e3,e1,e2]

One thing to note was that criteria query implemented sorting at the database end.
After googling around, I came across a script that had a SQL function which converted the field value to string that can alphanumerically sorted easily at database end.

To migrate the functions in script to database you have to unzip the script and write following command on terminal:

mysql -u username -p database_name < /path/to/unzipped/directory/natsort.install.mysql

Now only thing I had to do was to get a derived field from the Grails property that had to be sorted alphanumerically which was easily possible as the Grails had ability to create transient field using formula (thanks to wonderful Blog by Uday). Thus I had to create a new transient field using SQL function in formula. So I did something like this :

class Employee {
    String empId
    String empIdSortField           //transient field
    static mapping = {
        empIdSortField formula: 'natsort_canon(empId, "natural")'
    }
}

Now if I had to get Employee objects are sorted by empId alphanumerically, what I would do is :

Employee.createCriteria().list {
    order('empIdSortField')
}

Hope this was helpful to you!

  • Share/Bookmark
Posted in Database, GORM, Grails

Mysql top just like we have top command in Linux

Posted by Anuj Aneja on October 8th, 2011
Once in my grails project i was facing the problem of freeze of server.When this happens tomcat becomes unresponsive.Later on by analyzing the thread dump we found that this problem might be due to the database and tomcat communication. So as to analyse how many connection are active at mysql end we found a query.

Show processlist;

Which gives us the result as:
Output:
+—–+——+———–+———————+———+——+——-+——————+

| Id  | User | Host      | db                      | Command | Time | State | Info             |

+—–+——+———–+———————+———+——+——-+——————+

| 242 | root | localhost |      test_db    | Query   |    0 | NULL  | show processlist |

+—–+——+———–+———————+———+——+——-+——————+

The output shows how many connections are active and their properties.
But for this you need to run the query again and again to analyse the connections information. For a better experience we can use mytop  which is very easy to install and just like the top in linux, simple steps need to be followed to make it work.

Step 1:Install mytop using the following command.

anuj@intelligrape:~$ sudo apt-get install mytop
Step 2:
After installation we need make a configuration file .mysqlconfig any where
e.g
anuj@intelligrape:~$ vim .mysqlconfig
user=

pass=

host=localhost

db=db_name

delay=5

port=330

6socket=

batchmode=0

header=1

color=1

idle=1

For more option you can refer to documentation of mytop .
Step 3:
Now just need run this command to see the mysqltop working.
anuj@intelligrape:~$ mytop ~/.mysqlconfig
output:

Thats it ! Internally it runs the query “show processlist” and is very elegant for analysing mysql connections.

This helps me! Hope this helps you guys!
Anuj Aneja

Intelligrape Software

  • Share/Bookmark
Tags: ,
Posted in Grails

A use case of Bitwise AND

Posted by Mohd Farid on April 15th, 2011

Recently, I used bitwise Anding in my grails project. I am sharing the approach that we followed by means of an example.


Suppose we have a domain class Person which can have multiple attributes like Smart, Intelligent, Talkative etc.

What sort of relationship comes to our mind when we see this?
I believe the obvious answer would be Person hasMany Attributes

Next, what if we want to optimize our design so that the searches on the Person becomes better.

Well, this was the question we were facing in our recent project where we finally used the bitwise AND.
We decided to have one field of type Long in our Person class. We named this as attributes . Soon, I ll be explaining why a long for Attribute…

We ensured that the Attributes are never going to be more than 20-30 . So, it became a nice candidate which could utilize the benefit of Bitwise ANDing.

We associated a binary weight to every instance of Attribute domain class.

The rows in the attribute table of our database looked somewhat like this

id | attribute      | weight
1 | Smart          | 1
2 | Talkative      | 2
3 | Intelligent     | 4
4 | Cooperative  | 8
5 | Ignorant       | 16

So, in order to save a Person who is Smart and Cooperative , we would set his attributes field.
weight of Smart + weight of Cooperative.
ie 1+8 = 9.

Suppose our Person table has some entries as follows:

id  | name  | attributes
1   | Tom   |  9
2   | Fred   |  12
3   | John   | 18

If we want to search a person who is Intelligent we need to have a final query like this

Select * from person where attributes&4 = 4
(4 is the weight of Attribute - Intelligent)

Search a person who is Cooperative and Talkative

Select * from person where attributes&2=2 and attributes&8=8
or more optimally,
Select * from person where attributes&10=10
(2 is the weight of Talkative and 8 is the weight of Cooperative)


Some points to be kept in mind before taking this approach:

There is a maximum limit of 64 values for the field on which we plan to do BitwiseANDing.
Bitwise AND is not directly supported by Hibernate, but we can add a custom function and dialect.



Hope this helped.

Thanks & Regards
Mohd Farid

  • Share/Bookmark
Posted in Database, Grails

Mysql Dump multipurpose script.

Posted by Hitesh Bhatia on January 12th, 2011

Taking MYSQL dump is usual requirement in my project . And Since some of the tables in project are huge so often I have to take dumps of selected tables and often have to ignore some tables.

since we had to do it frequently in our project so I created a script  which does following for me.

  1. Takes database dump
  2. Takes dump of specified tables.
  3. Takes dump of all the tables other than specified.

Inside the script I have added variables like username and password which are required by Mysql. (So if you are using this ,you will need to change them.)

USER="username"
PASSWORD="password"
DATABASE="$1"
DUMPS="Dumps"
DUMPDIR="$DATABASE$DUMPS"
FILENAME="latest.sql"
cd;mkdir "$DUMPDIR"

if [ $# -eq 0 ]; then
	echo "Usage : <databasename> <tablename*>  # if table name(s) is specified only that/those table will be dumped"
	echo "Altername Usage : <databasename> -k <table(s)dToBeIgnored>"
	exit
fi

if [ $# -eq 1 ];then
echo "Dumping $DATABASE to  $DUMPDIR/$FILENAME"
mysqldump --user="$USER" --password="$PASSWORD" "$DATABASE" > ~/"$DUMPDIR"/"$FILENAME"
fi

COMMAND="mysqldump --user=$USER --password=$PASSWORD $DATABASE"
if [ $# -gt 1 -a $2 != "-i" ];
	then
     for i in $*
	 do
		 if [ $i != "$DATABASE" -a $i != "-i" ];then
			 $COMMAND $i > ~/"$DUMPDIR"/"$i.sql";
		 fi
	 done
fi

if [ $# -gt 2 -a $2 == "-i" ];
	then
	COMMAND2=" "
	for i in $*
	do
		if [ $i != "$DATABASE" -a $i != "-i" ];then
		COMMAND2="$COMMAND2 --ignore-table=$DATABASE.$i"
		fi
	done
	$COMMAND $COMMAND2 >  ~/"$DUMPDIR"/"$DATABASE"-"Ignored.sql"
fi

Now what this can be used in three ways -
(Note – I have named this script sqlDump and all of following examples use this name )

  1. ./sqlDump.sh databasename
    This will take a complete dump of all my tables in specified database and store it in file named latest.sql
  2. ./sqlDump.sh databasename table1 table2 … tablenN
    This will take dump of all tables specified.And will store them in file named – table1.sql , table2.sql … tableN.sql
  3. ./sqlDump.sh databasename -i table1 table2 .. tableN
    This will take dump off all the tables in database ignoring specified tables. And will store it in file named <databaseName>-Ignored.sql.

If file is executed without any arguments it will just describe its Usage
Note – All of these tables will be stored in directory named <databasename>Dumps in home directory.

(more…)

  • Share/Bookmark
Posted in Grails, Linux

MySql : Publish results of a query to a text file

Posted by Himanshu Seth on August 16th, 2010

A while back I was trying to debug an error in our application logic. For that I had to analyze the results of a query

This analysis was getting pretty tough to do on the command line. So, I tried to find a way to transfer the results of a query to a text file so that they can be analysed easily.

With help from google, I got my answer at http://www.wellho.net/forum/The-MySQL-Relational-Database/MySQL-query-to-a-text-file.html

echo "<YOUR SELECT QUERY>"| /usr/local/mysql/bin/mysql --user=root --password=password dbName > /path/to/your/textFIle.txt/

Hope this helps

  • Share/Bookmark
Tags:
Posted in Database

Changing Location of MySql DataStore

Posted by Vivek Krishna on June 7th, 2010

On one of our projects, we had to store the MySql Data at a location different from the default location. This was needed because the server was on an instance-store Amazon instance which meant that the data would get lost if the instance had to be rebooted for some reason. After going through the tutorials and articles on AWS, we came across this excellent piece on how to setup MySql Data store on an EBS volume.

Later on, I tried out the steps on the local machine and see if we could move the MySql data store to some other location on our system. As it turned out, we could and here are the steps I followed.

Stop mysql server running on your system with the command

sudo /etc/init.d/mysql stop

Now, create folders named etc, lib and log at any location where you wish to save your mysql data. Let us keep them inside a folder called “/mysqldata”

mkdir etc lib log

Now, back-up your mysql data by copying the contents of /etc/mysql, /var/lib/mysql and /var/log/mysql to another location. Then, move the folders /etc/mysql, /var/lib/mysql and /var/log/mysql to the “/mysqldata”

mv /etc/mysql /mysqldata/etc
mv /var/lib/mysql /mysqldata/lib
mv /var/log/mysql /mysqldata/log

Let us now create new folders /etc/mysql, /var/lib/mysql, /var/log/mysql

mkdir /etc/mysql
mkdir /var/lib/mysql
mkdir /var/log/mysql

Now these new directories need to be mapped with the ones we moved to “/mysqldata”
This is done by setting the mount point for each of these directories to the ones which we created.
To do so, appending the following entries to “/etc/fstab”

mkdir /etc/mysql
/mysqldata/etc/mysql /etc/mysql none bind
/mysqldata/lib/mysql /var/lib/mysql none bind
/mysqldata/log/mysql /var/log/mysql none bind

With this done, let us mount all the entries by issuing the command

mount -a

MySql data store is now at another location.

Hope this helps

  • Share/Bookmark
Tags: , ,
Posted in Database, Linux

Backup & Restore MySql Database Table(s)

Posted by Amit Jain on April 23rd, 2010

Hi Friends,

I needed to take the backup and restore the selected table(s) of mysql database. And I found Gaurav Chauhan’s blog on taking dump of the database and to restore the database. So I tried with the same commands but with table names passed as parameters and that worked.

mysqldump -u root -p my_database Table1 Table2 > /home/amit/tablesDump.sql;

And the file the dump of Table1 and Table2 got created at /home/amit/tablesDump.sql.

To restore the table(s) back, we used the following command

mysql -u root -p my_database_2  //First we need to login to mysql
 
mysql> source /home/amit/tablesDump.sql;

Both tables were created in my_database_2.

Hope this helped.

Cheers!
~~Amit Jain~~
amit@intelligrape.com

http://www.intelligrape.com

  • Share/Bookmark
Posted in Database