Mysql notes

From OriWiki

Jump to: navigation, search

documentation: http://dev.mysql.com/doc/refman/5.0/en/index.html

Contents

connect

mysql -u root -p
mysql -u root -p<pass> <database>

creating a database

(is this necessary to be executed by root? GRANT ALL ON menagerie.* TO 'your_mysql_name'@'your_client_host';)

CREATE DATABASE menagerie;

-- use it

use ori_db;

create table

CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),

   -> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

describe pet;

insert to table

  • insert one line:

INSERT INTO pet

   -> VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);

add a selection to existing table: insert into tmp1 select name,species,sex from pet where species = 'dog';

select

SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;

delete

delete from pet;

load

load data local infile '/pulsar/tmp/pet.txt' into table pet;

load data local infile <path> into fields terminated by ',';

misc

mysql> SHOW DATABASES;

show tables;

  • clear the entire table but don't delete it
truncate table
mysql -u root -pskaya7 < check.sql file check.sql: use ori_db; select * from pet; select 7+2,6,18; mysql -t -u root -pskaya7 < check.sql mysql -vvv -u root -pskaya7 < check.sql source check.sql SELECT article, dealer, price FROM shop WHERE price=(SELECT MAX(price) FROM shop); SELECT article, dealer, price FROM shop ORDER BY price DESC LIMIT 1; delete a table drop table
delete a data base drop database <database>

delete lines from table

delete from
where <field> = <value>;
  • count total number of raws..
select count(*) from opentick_trades; obtain only <num> rows: select * from
limit <num> select date,addtime(time,'-05:00:00') as time ,symbol,.... create a new table from a selection: create table
select ... create table <new one> select * from <orig>; RENAME TABLE tbl_name TO new_tbl_name select symbol,min(date) as mdate from td where date !='0000-00-00' group by symbol order by mdate;

tables

opentick_trades - all trades data from opentick tickdata_minutes_gmt - all minutes data from tickdat GMT time td - same as about but GMT-5; td_open - same as above but between 9:30-10:00:


update pet set owner = 'yosi' where owner = 'ori';

select t1.date,t2.date from ews_3 t1,ews_13 t2 limit 6;

select avg(open) from ews_3;


set sabstruction:

select date from ews_13 where date != all(select date from ews_3);

when does the trade starts? select date,min(time) from market_data.td where time between '09:30:00' and '10:00:00' and symbol = 'ews' group by date limit 6;

applying a function: select time,basket * 10+pow(2,2) as yofi from td_open limit 6;

using variables: select @delta:=5; select @delta from td_open limit 6;

counting the symbols select count(t1.symbol) from (select symbol from td_open group by symbol) t1;


show times after delta:

select date,time,open from td_open where symbol = 'ews' and time = addtime('09:30:00',@delta) limit 6;

only relevant delta .. select t1.date,t1.time,t1.open from td_open t1, open_times_ews t2 where t1.symbol = 'ews' and t1.time = addtime('09:30:00',@delta) and t1.date = t2.date and t2.open_time < '09:32:00' limit 6;

how to construct open+delta information

1. select the line of open+delta for specific symbol: create table ews_plus_5 select * from market_data.td_open where time = addtime('09:30:00','00:05:00');

2. create table of actual open times: create table ews_open_times select date,min(time) as open_time from market_data.td_open where symbol = 'ews' group by date;

3. select the open+delta of those days that doesn't start late. select t1.date,t1.time,t1.open from ews_plus_5 t1, ews_open_times t2 where t1.date = t2.date and t2.open_time < '09:32:00' limit 6;

using opentick data

multi grouping: select date,basket,count(basket) from opentick_trades group by basket,date limit 6;

using case in order to count different things... select date,sum(case when time <'10:01:00' then 1 else 0 end) as smaller,sum(case when time >'10:01:00' then 1 else 0 end) as bigger from tmp group by date;

select sum(case when basket=1 then 1 else 0 end) as basket1,sum(case when basket=2 then 1 else 0 end) as basket2 from opentick_trades group by date limit 6;

create table basket_cmp select date,sum(case when basket=1 then 1 else 0 end) as basket1,sum(case when basket=13 then 1 else 0 end) as basket13, sum(case when basket=226 then 1 else 0 end) as basket226, sum(case when basket=265 then 1 else 0 end) as basket265 from opentick_trades group by date;

sql 2 csv

select * into outfile '/tmp/tt1.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from open_2 order by date;

select * into outfile '/tmp/open_2.csv' fields terminated by ',' lines terminated by '\n' from open_2 order by date;

include titles (headers field names ...) select 'title1', 'title2' union select * into outfile '/tmp/cc.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from tt;

== connection to matlab ==documentation: http://dev.mysql.com/doc/refman/5.0/en/index.html

connect

mysql -u root -p mysql -u root -p<pass> <database>

creating a database

(is this necessary to be executed by root? GRANT ALL ON menagerie.* TO 'your_mysql_name'@'your_client_host';) CREATE DATABASE menagerie;

-- use it use ori_db;

create table

CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),

   -> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

describe pet;

insert to table

  • insert one line:

INSERT INTO pet

   -> VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);

add a selection to existing table: insert into tmp1 select name,species,sex from pet where species = 'dog';

select

SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;

delete

delete from pet;

load

load data local infile '/pulsar/tmp/pet.txt' into table pet;

load data local infile <path> into
fields terminated by ',';

misc

mysql> SHOW DATABASES;

show tables;

  • clear the entire table but don't delete it
truncate table
mysql -u root -pskaya7 < check.sql file check.sql: use ori_db; select * from pet; select 7+2,6,18; mysql -t -u root -pskaya7 < check.sql mysql -vvv -u root -pskaya7 < check.sql source check.sql SELECT article, dealer, price FROM shop WHERE price=(SELECT MAX(price) FROM shop); SELECT article, dealer, price FROM shop ORDER BY price DESC LIMIT 1; delete a table drop table
delete a data base drop database <database>

delete lines from table

delete from
where <field> = <value>;
  • count total number of raws..
select count(*) from opentick_trades; obtain only <num> rows: select * from
limit <num> select date,addtime(time,'-05:00:00') as time ,symbol,.... create a new table from a selection: create table
select ... create table <new one> select * from <orig>; RENAME TABLE tbl_name TO new_tbl_name select symbol,min(date) as mdate from td where date !='0000-00-00' group by symbol order by mdate;

tables

opentick_trades - all trades data from opentick tickdata_minutes_gmt - all minutes data from tickdat GMT time td - same as about but GMT-5; td_open - same as above but between 9:30-10:00:


update pet set owner = 'yosi' where owner = 'ori';

select t1.date,t2.date from ews_3 t1,ews_13 t2 limit 6;

select avg(open) from ews_3;


set sabstruction:

select date from ews_13 where date != all(select date from ews_3);

when does the trade starts? select date,min(time) from market_data.td where time between '09:30:00' and '10:00:00' and symbol = 'ews' group by date limit 6;

applying a function: select time,basket * 10+pow(2,2) as yofi from td_open limit 6;

using variables: select @delta:=5; select @delta from td_open limit 6;

counting the symbols select count(t1.symbol) from (select symbol from td_open group by symbol) t1;


show times after delta:

select date,time,open from td_open where symbol = 'ews' and time = addtime('09:30:00',@delta) limit 6;

only relevant delta .. select t1.date,t1.time,t1.open from td_open t1, open_times_ews t2 where t1.symbol = 'ews' and t1.time = addtime('09:30:00',@delta) and t1.date = t2.date and t2.open_time < '09:32:00' limit 6;

how to construct open+delta information

1. select the line of open+delta for specific symbol: create table ews_plus_5 select * from market_data.td_open where time = addtime('09:30:00','00:05:00');

2. create table of actual open times: create table ews_open_times select date,min(time) as open_time from market_data.td_open where symbol = 'ews' group by date;

3. select the open+delta of those days that doesn't start late. select t1.date,t1.time,t1.open from ews_plus_5 t1, ews_open_times t2 where t1.date = t2.date and t2.open_time < '09:32:00' limit 6;

using opentick data

multi grouping: select date,basket,count(basket) from opentick_trades group by basket,date limit 6;

using case in order to count different things... select date,sum(case when time <'10:01:00' then 1 else 0 end) as smaller,sum(case when time >'10:01:00' then 1 else 0 end) as bigger from tmp group by date;

select sum(case when basket=1 then 1 else 0 end) as basket1,sum(case when basket=2 then 1 else 0 end) as basket2 from opentick_trades group by date limit 6;

create table basket_cmp select date,sum(case when basket=1 then 1 else 0 end) as basket1,sum(case when basket=13 then 1 else 0 end) as basket13, sum(case when basket=226 then 1 else 0 end) as basket226, sum(case when basket=265 then 1 else 0 end) as basket265 from opentick_trades group by date;

sql 2 csv

select * into outfile '/tmp/tt1.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from open_2 order by date;

select * into outfile '/tmp/open_2.csv' fields terminated by ',' lines terminated by '\n' from open_2 order by date;

include titles (headers field names ...) select 'title1', 'title2' union select * into outfile '/tmp/cc.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from tt;

connection to matlab

function example()

   mysql( 'open','localhost','root','skaya7');
   mysql('use','ori_db');
   mysql('show tables');
   mysql('select * from shop'); % [a,b,c] = mysql('select * from shop');

end

indexing

Indexing is aimed to improve efficiency of information retrieval in cost of maintaining an additional table

create index index_date_opentick_trades on opentick_trades (date); building the index took a long time (<> 5 min) for a 6 million lines of the table. now, the following is much quicker:

select * from opentick_trades where date between '2006-5-10' and '2006-6-15' limit 7; 

boost of <> X15

function example()

   mysql( 'open','localhost','root','skaya7');
   mysql('use','ori_db');
   mysql('show tables');
   mysql('select * from shop'); % [a,b,c] = mysql('select * from shop');

end

indexing

Indexing is aimed to improve efficiency of information retrieval in cost of maintaining an additional table

create index index_date_opentick_trades on opentick_trades (date); building the index took a long time (<> 5 min) for a 6 million lines of the table. now, the following is much quicker:

select * from opentick_trades where date between '2006-5-10' and '2006-6-15' limit 7; 

boost of <> X15


building from a backup.sql

mysql -u root -p****** -h localhost ori < backup/wikidb.sql
(the wikidb.sql can be downloaded from the backup utility of cpanel)
Personal tools