您的位置:首页 > 数据库 > MySQL

mysql常用命令 - 笔记

2015-01-06 17:51 387 查看
mysql:一个schema对应一个库。

oracle:一个Instance实例就是一个库,对应于Mysql的service来管理数据库。可以建立多个schema,这是对象的集合。

两者都可以建立两个service和实例,但需要区分不同的文件名。否则会有冲突,尤其在Mysql中。

set oracle_id=这个是设置oracle的实例选择。

cd D:\mysql\mysql-5.5.31-win32

启动:在D:\mysql\mysql-5.5.31-win32\bin>mysqld --console

mysqld --showdown

常用命令:

登陆 mysql -uroot -p 无密码

mysql -uroot -e "select version()"

mysql -uroot -B/-H -e "select version()" 可以方便输出不同形式。

use mysql;--该表中放了很多数据库的管理数据。

show databases;--可以在使用mysql库之前来看。

show tables;--可以切换库来看

desc user;该表的细节。

desc dba_user;

desc dba_object;

desc dba_tables;

select table_name from dba_tables

上面几个desc.mysqL和oracle应该都是可以的。

status

mysql -v;

drop user ''@'localhost';

select @@datadir;

ctrl+c会终止显示。

--创建新库。

create database world_innodb;

show create database db_test\G,显示数据库字符集。竖着显示。

use **;

source d:/**;

sqlmode:缺省无值。

select @@sql_mode;

set sql_mode='';清空

set sql_mode=0;无关警告需要关闭。

set sql_mode='pipes_as_concat';

set sql_mode='ignore_space'; 忽略函数名与()之间的空格。

set sql_mode='ignore_space,traditional'如此设置也可以。

比如ansi,only_full_group_by,traditional等。传统的不会截断。

create table t2(id int primary key auto_increment, name varchar(10));

create table t3(i int default null, j int not null,k int default -1); 非传统模式是会自动转换。

insert into t(i,k) vakyes(1,2);

show warnings;

show errors;

D:\mysql\mysql-5.5.31-win32\bin>perror 1364 这里是看错误的手册。

drop table if exists t123;

float(10,2)总共十位,2位小数。否则全部越界被转换值了。

bit(4),插入用b'1111'

create table t4(i int , j bit(4));

insert into t4(i,j) values(2,b'1000');

integer类型:

枚举ENUM

continent enum('asia','europe');

集合SET

continent SET('asia','europe')

create table sets (name varchar(50),colors set('red','blue','green'));

insert into sets values('sar','red,blue');

字符集

show collation 这个是排序的方式。

show character set;--查看支持的字符集,可以在库表列指定 。

utf-8 3个字符。

修改字符集

alter database db_test character set keybcs2

collate latin2_czech_cs;

create database mydb charset gb2312;存储的时候按照设置来做,但显示需要看你的系统有没有支持这种显示。

DATA:

TIME/

year/year(4),year(2)

date/年月日

datetime/8个长度。到时分秒

timestamp不能超过1970-01-01到2037。只能到秒。

create table birthdates (name varchar(50),bdate date);

insert into birthdates values('sdr','1950-02-15');

create table birthdates2 (name varchar(50),bdate timestamp);

insert into birthdates2(name) values('str');--timestamp会自动填充当前日期。

select * from birthdates2;

表达式:

select name,truncate(population/surfacearea,2) as'tt',if(t>t2,'神气','不生气');

select 1+'1'

select name "emp name" from country ;列别名不要用单引号。

select 'able'||'def';默认值是0,设置后为连接符了。

set sql_mode='pipes_as_concat';

select 'Hello'='hello';默认不区分。

select name from city where name rlike '^new.*rk$';

*多次

?一次或者多次。

\\\\第一个和第三个是转义字符换成[\\]

select '//' = '////';

select '//' rlike '[//]';

interval 10.

select '2010-10-10'+ interval 10 day;

if(code='usa',1,2)

select if(1>0,'yes','no') from dual;返回YES和NO.

NULL 不参与=><运算,只参与is。

select case when code='usa'THEN 'UT'

WHEN CONTINENT='europe'then'e'

else 'last'

end as area ,sum(gnp),sum(population) from country group by area;

返回位置。长度LENGTH,CHAR_LENGTH?

substring_index('as tt dd ','tt' 1);

insert('addaf dfasf dd',6,5,'tttt');从6到后面5个字符换成了TTTT.

MAKEDATE(2012,125)加日期

MAKETIME()加时间

ISNULL(0);

SELECT IFNULL(NULL,'A')返回第一个不为空的值。

SELECT IFNULL(0,'A'),CONCAT('A','B')--AB,CONCAT('A',null,'B')返回NULL,如果是CONCAT_WS则CONCAT_WS(‘A’,NULL,'B')也为AB.

INFORMATION_SCHEMA

SELECT TABLE_NAME FROM INFORMATION_SCHEMA ORDER BY

STATISTICS 统计表

select table_name,engine from tables where table_schema='world_innodb';

show full tables加上视图。

mysql> select index_schema,index_name from STATISTICS where table_name='city';

show index from city

D:\mysql\mysql-5.5.31-win32\bin>mysqlshow -uroot -p world_innodb

D:\mysql\mysql-5.5.31-win32\bin>mysqladmin -uroot -p shutdown

单列不可分,不可传递性,

oltp系列.速度快,3F规范,修改方便容易。jion开销大。innodb.

olap查询性能提高,数据集市,数据仓库。myisam

show variables like 'lower%';缓存中的。存的时候是大写还是小写?

show global variables like 'lower%';全局的。

--建立表

create table copycity select * from city;

desc copycity;

create temporary table texas as select * from city limit 2;临时表,主要用出报表。

select * from texas;

oracle临时表的结构还在,只是数据没有了。基于事务的,提交后数据就没有了。基于会话的。退出的话,数据就结束了。

mysql的基于会话的,一断了就结束了。连接池技术的话,这个会话是不会断的。基于客户端/服务器模式的话,会话结束数据就断了。

外键的级联删除cascade.

只有InnodB支持外键。

外键约束,在列后面加上逗号或者不加然后加上外键约束。含义上一个是表级别约束一个是列级别约束。其实使用都一样。

create table auto_test(id int not null auto_increment, name varchar(10),primary key(id));

insert into auto_tests (name) values ('pat'),('sss');多行插入。oracle没有。

目的是啥?这里的账户是账户,而oracle的账户就是schema.

create user 'aa'@'192.168.1.%' identified by '123';

grant all on world_innodb.* to 'aa'@'192.168.1.%';可以用%.%.%.%来代替。

对方授权,我方访问。

mysql -uaa -p -h 192.168.1.2 -P 3308

insert into dd(id,name)values(1,'dt') on duplicate key 如果有重复则更新,否则插入,其实这个操作是先让你操作,然后判读是否重复,如果重复再放弃。

update name=repeat('9',9);老的一行其实是删除了,然后再插入新的一行。repeat

事务。

select @@autocommit;

set automommit=off set session autocommit =off.

最容易的方式是在My.ini中改。

insert/update/。。。需要显示commit;DML,DDL,授权等会直接提交。DML+DDL则会自动提交。

start transaction

commit;

SHOW ENGINES\G.

脏读,在Oracle中不会出现。因为其默认的是读提交的隔离级别。何为脏读,A事物,还没有提交,在B事务看不到。一旦A提交B马上就能看到.而mysql是重复读的数据。在B事物中一直以来读的数据都是开始的那个数据,即便A事物提交后还是看不到。除非B事务提交,重新搞一个事务,这样才能查到A已提交的事务。

串行隔离级别,select以后,别人是不可以改的.mysql的事务默认是可repeat读的。但其支持的是四种。

select @@tx_isolation;

select @@global.tx_isolation,@@session.tx_isolation;

大众票不要用FOR UPDATE,而为领导留的票则可以用。

update city set name='ccc'where id=1;

死锁:相同资源重复等待。

内连,inner join,外键带索引推荐使用。就是平时的a.id=b.id。

外连,outer join,oracle支持full outer join.

cross join一般不用。笛卡尔积。N*N.不加条件的链接多张表的一次查询,如select t.* a.* from city t, country a。

self join。

anti join.反连接

semi join.半连接

起了别名就不能用其原来的名字了。

select * from city join country on id=cid where city.name='tt';

mysql对于hash ,sort-merge的关联背后的数据模型,支持不足,需要通过应用层来处理复杂的查询关联,而oracle是支持这样的 模型的。

多表更新和多表删除。oralce 里面是没有的。不保证原子性,不支持order by 和limit.

delete country,city from 。。。

update city ci inner join country co on ci.countrycode = co.code set ci.population =ci.population+20000,co.population=co.population+20000 where co.name ='brazil' and ci.name ='rio de janeiro'

select co.gnp, ci.name, ci.population, ci.district, cl.language

from city ci

join country co on co.code = ci.countrycode

and co.name = 'china'

join countrylanguage cl on ci.countrycode = cl.countrycode

and cl.isOfficial = 'T'

order by ci.population desc

limit 20;

select sum(population) from country;

select avg(cont_sum) from

(select continent, sum(population) as cont_sum

from country group by continent) as t;

select * from country

where not exists (select null from city where countrycode = code);

--子查询不要写的复杂,不擅长复杂的子查询。

视图,操作后插入到基表。

create view xx as select where >= 100000 with check option;后面的检查一定要加上where.

alter view ***

select * from information_schema.view;

show create view cityviwe\G

show full tables from world_innodb;//展现VIEW

show create view cityview;

预备sql语句,只在session有效,作为一个对象,可以节约带宽。减少分析次数。

写在存储过程中。

prepare my_stmt from 'select count(*) from countrylanguage where countrycode=?';

set @code='ESP';

execute my_stmt using @code;

deallocate prepare my_stmt;

prepare namepop from 'select name,population from country where code=?'

select * into outfile 'd:/city.txt' from city;//

select * into outfile 'd:city.csv' fields terminated by ',' enclosed by '"' lines terminated by '\r' from city;

truncate table city;删除

load data local infile 'd:/city.txt' into table city;//local不会中断,而在service中是会中断的。

mysqldump,导出,

mysqldump -uroot -p world_innodb >d:\world.txt

mysqldump -uroot -p -tab world_innodb >'d:/world.txt'

mysqlimport

字符集、权限问题攻关,大小写。

create function hello(input char(20))

returns char(50)

return concat('hello,',input,'!');

select hello('world');

存储过程与函数区别:存储过程返回值要定义在参数里,函数一定要带返回值。存储过程要用call

函数不可返回复合类型。

compound内容段:

delimiter// 确定是一个分隔符,可以定义任何符号,下面再还原回';'。

....

delimiter;

delimiter //

create function pop_percentage(c_code char(3))

returns DECIMAL(4,2)

begin

declare worldpop, cpop BIGINT;

select sum(population) from country into worldpop;

select population from country where code =c_code into cpop;

return cpop/worldpop*100;

end //

delimiter ;

select pop_percentage('CHN');

show create function hello\G

DORP FUNCTION IF EXISTS hello2you;

show function status where db='world_innodb'\G

定义者权限:缺省,如果定义者授权给用户,则以定义者所拥有权限为执行。哪怕本身对这个表没有访问权限。

调用者权限:以调用者权限来执行,可能对表没有权限,那么就不行了。

delimiter //

create procedure dorepeat (p1 int)

begin

declare var_x int;

set var_x=0;

repeat set var_x = var_x +1;

select concat('the x variable is', var_x) as Repeat_Counter;

untill var_x >p1

end repeat;

select concat('the final repeat number is',var_x)

as repeat_results;

end //

delimiter;

delimiter //

create procedure dorepeat (p1 int)

begin

declare var_x int;

set var_x=0;

repeat set var_x = var_x +1;

select concat('the x variable is', var_x) as Repeat_Counter;

until var_x >p1

end repeat;

select concat('the final repeat number is',var_x)

as repeat_results;

end //

call dorepeat(10);

show variables like '%bin%';

只要在log_bin如果打开的话,就需要在函数()中设置输入的配置是一般设置sql\no_sql,determine;

要求是确定数据,一般不合适。

在配置文件中去关闭和开启,my.ini里面,log_bin=mysql-bin,然后重启service。

默认的配置文件是没有打开的,而新建的自己的my.ini是在my-medium拷贝的,是有已经打开的。

declare null_not_allowed condition for 23000;

create table d_table(s1 int,primary key (s1));

delimiter //

create procedure dohandler()

begin

declare dup_keys condition for sqlstate '23000';

declare continue handler for dup_keys set @garbage=1;

set @x=1;

insert into world_innodb.d_table values(1);

set @x=2;

insert into world_innodb.d_table values(1);

set @x=3;

end//

delimiter ;

select @x;

select @garbage;

create table dotrigger(c1 int);

create table au_triggers(o_c int,n_c int,d_c datetime);

create trigger dotrigger_ai after insert on dotrigger for each row

insert into au_triggers (n_c,d_c) values(NEW.c1,now());

create trigger dotrigger_au after update on dotrigger for each row

insert into au_triggers (o_c,n_c,d_c) values(OLD.c1,NEW.c1,now());

update dotrigger set c1=10 where c1=1;

此处的new是改后,old是改前。

insert into dotrigger values (1),(2),(3);

use information_schema

desc triggers;

mysiam没有外键,可以用trigger来模拟。

存储引擎:只有Innodb有外键。

show variables like '%engine%';

5.1版本默认是innodb。mysql cluster 用于电信计费,爱立信提供。memory全部放在内存中。

建表才能指明存储引擎。

排前三:innodb\myisam\memory. mrg_myisam,是merge所有myisam表。

show engines\G

修改表的引擎最好是复制一份,然后再改。alter table t engine=memory;

select table_name,engine from information_schema.tables where table_schema='world_innodb'\G

show table status like 'city';

show create table user\G;

show tables;

mysiam表可以拷贝到任何地方,然后mysql可以直接认知他,并正常使用,便携性太高。

show variables like 'key_buffer%';

存储引擎在底层不通信,只在读到服务器内存来通信,在表的缓冲就是文件系统的缓冲,设置大一点,可以提高性能。

mysiam最好要通过命令关闭,会写到磁盘的。防止出现数据丢失问题。而直接关闭是没有rollback机制的。

innodb_data_file_path =ibdata1:10M:autoextend。共享表空间,只有一个以ibdata,可以扩展,但只能放一个。添加新的ibdata需要在这个语句修改后,重启服务。

show variables like '%innodb%';

set global innodb_file_per_table=on;

Innodb 的数据存储方式能够通过配置来决定是使用共享表空间存放存储数

据,还是独享表空间存放存储数据。独享表空间存储方式使用“.ibd”文件来存放数据,且

每个表一个“.ibd”文件,文件存放在和MyISAM 数据相同的位置

memory索引 可以支持hash索引,比B树快。但不能支持范围查找。

可以用myisam表,innodb表的数据创建一张Memory表,在服务器启动的时候的跑,imit_file参数里跑。

这样就可以使用memory表的等值查找了。

其他存储引擎:archive:只能insert、select 不能修改。备案引擎。csv引擎支持保存到csv

create table city2 like city;

desc city2;

insert into city2 (name,countrycode,district,population)

select name,countrycode,district,population from city2;

innodb插入时间:

0.3

0.48

0.2

0.41

Query OK, 16316 rows affected (0.41 sec)

Query OK, 32632 rows affected (0.67 sec)

Query OK, 65264 rows affected (2.26 sec)

Query OK, 130528 rows affected (5.20 sec)

Query OK, 261056 rows affected (11.78 sec)

Query OK, 522112 rows affected (26.91 sec)

Query OK, 1044224 rows affected (57.92 sec)

Query OK, 2088448 rows affected (2 min 20.04 sec)

create table city3 like city;

alter table city3 engine=myisam;

insert into city3 (name,countrycode,district,population)

select name,countrycode,district,population from city3;

--myisam插入时间:查询count(*)不要时间,原因是表中已经统计好这个值了。

Query OK, 16316 rows affected (0.11 sec)

OK, 65264 rows affected (0.41 sec)

OK, 130528 rows affected (0.76 sec)

OK, 261056 rows affected (1.58 sec)

OK, 522112 rows affected (3.70 sec)

OK, 1044224 rows affected (14.07 sec)

OK, 2088448 rows affected (35.13 sec)

OK, 4176896 rows affected (1 min 5.52 sec)

mysql> select count(*) from city3;

+----------+

| count(*) |

+----------+

| 8353792 |

+----------+

1 row in set (0.00 sec)

mysql> select count(*) from city2;

+----------+

| count(*) |

+----------+

| 4176896 |

+----------+

1 row in set (19.67 sec)

create table city4 like city;

alter table city4 engine=archive;//此处这样写不对的原因在于多了个索引了。

show create table city4;

--MYISAM

CREATE TABLE city5 (

ID int(11) NOT NULL AUTO_INCREMENT,

Name char(35) NOT NULL DEFAULT '',

CountryCode char(3) NOT NULL DEFAULT '',

District char(20) NOT NULL DEFAULT '',

Population int(11) NOT NULL DEFAULT '0',

PRIMARY KEY (ID)

) ENGINE=ARCHIVE AUTO_INCREMENT=8353793 DEFAULT CHARSET=latin1

insert into city5 (name,countrycode,district,population)

select name,countrycode,district,population from city5;

Query OK, 65264 rows affected (0.48 sec)

Query OK, 130528 rows affected (0.92 sec)

Query OK, 261056 rows affected (1.70 sec)

Query OK, 522112 rows affected (3.48 sec)

Query OK, 1044224 rows affected (6.77 sec)

Query OK, 2088448 rows affected (13.29 sec)

OK, 4176896 rows affected (25.10 sec)

explain select count(*) from city2 where id='kabo1';按索引。

mysql> select * from city2 where id=123;

1 row in set (0.03 sec)

mysql> select * from city3 where id=123;

1 row in set (0.06 sec)

mysql> select * from city5 where id=123;

Empty set (3.56 sec)

explain select count(*) from city2 where district='kabo1';不带索引。

explain select count(*) from city3 where district='kabo1';不带索引。

explain select count(*) from city5 where district='kabo1'缩影索引。

总结三种引擎,越来越快

索引,对批量更新的慢三倍以上,所以只建立必要的需要定位的索引。

mysiam支持fulltext索引。SPATIAL地理信息索引。

mEMERY是支持HSH/b-tree。

innodb :b-tree/ cluster index是内部的组织结构,是表内建的,只能利用它,不能创建它,插入时候按照主键来做就会更快,这是利用cluster index的地方。

这里没有位图索引。也没有反序索引。

前缀索引,B-TREE,前面N个字符,接近整个列的选择性,所以就可用前缀索引了。index(name(10)).innodb,Mysiam.都支持前缀索引。

尽量不要用null值,过度索引不需要,重复索引不可要,

选择性:selectivity.返回重复值越少则越高。

show index from actor\G;

复合索引,一定要使用它的开头的那个列,直接用第二个列是不会生效的。(a,b,c)有效的只能有a,ab,abc,ac都可能不行。还可以用max(a),可以直接返回顺序,不用排序。select b from t order by a.

oracle index skip新版本oracle是支持这种直接用第二个列的,但最好不要用,开销比较大。

全文索引:FULLTEXT(title)

where match(title) against('prince +anne')查找这个单词。不会考虑单词出现的顺序,+号是至少出现一次,-号是不能出现这个单词。

explain select * from t where year(d)>=1994,带函数的列,除非是建立函数索引,否则即便列上有索引也无效。

type:

SYSTEM一定是一行。效率是最高。

const,常量放回一个值。explain select * from city where id=3082\G;可能是一行。

eq_ref,两张表的主键关联。

ref,

ALTER TABLE city add index (district);

create index idx on city(id,name);

explain select * from city where district='california'\G;

index_merge,

unique_subquery单行

index_subquery多行

index是索引的全扫,比表的全扫要好多了。

range

all

key

ref:引用

show variables like 'sort%'每个session分配一个sort_buffer_size空间留排序用,空间不够的时候会以mysiam表的方式放到磁盘上。

排序、join开销是关系数据库中开销非常大的两个。

最好不要用* 而是选择必要的列,否则sort空间开销大。

like'%ee%'索引不可用。用't%';

length(XXX),不支持建函数索引,但可以建立一个列,用触发器来更新值,然再在这个列建索引。

用Limit。

建立统计表。方便统计数据展示。

一个事务做的事情太多,会阻塞,太少吞吐量变小,因为每个操作都会写日志等操作,占用带宽。

varchar变长大小灵活性好,char固定大小性能好。

索引、有效的sql,最优化的存储引擎,总结表四者来优化。

返回行很多,很可能走全盘扫,返回很少,则走索引。

alter table fo2 add primray key (c1);

alter drop,可以删几个索引,drop只能删一个。

DROP INDEX `PRIMARY` ON fo2;引号在键盘左上角。

DROP INDEX XX ON TABLE;

执行计划。5.5更好支持多CPU的结构。mutex内存锁。Innodb才有行锁。事务,读写分离更好。

--查有多少索引。

select index_name,column_name,seq_in_index,

cardinality,index_type

from information_schema.statistics

where table_name='city' and table_schema='world_innodb';

--看单个预计执行情况。here district='california'\G;

select name from city where id>10;这个语句不走索引。

explain select name from world_innodb.city where id between 10 and 50 order by name\G;因为name没索引,所以会导致Using filesort。加个ID,NAME的复合索引。则order by id,name才是OK的。原语句是不行的。

order by 必须要加复合索引的第一位。

create index idx2 on city (name,population);

explain select name,population from world_innodb.city where population>1000000 order by name\G;

额外提示:

using where;

using index;这句话表示查询结果集在复合索引内存中直接返回。不查表。如果查列再加一列,则复合索引满足不了,需要查表。

Using filesort。排序,如果是复合索引就直接通过上面Index方式直接排序好返回了。

explain select language,percentage from countrylanguage where countrycode='GBR' ORDER BY language\G;

select index_name,column_name,seq_in_index,

cardinality,index_type

from information_schema.statistics

where table_name='countrylanguage' and table_schema='world_innodb';

show variables like '%coll%';

set collation_connection=latin1_general_cs;--设大小写敏感。不够,会话,因为下面的列还是不敏感。

show full columns from country where field='code'\G

建库、表的时候就直接是大小写敏感。

create table country2 as select * from country;

alter table world_innode.country2 modify code char(3) collate latin1_general_cs;--把列变为敏感的

压力测试:

100个用户,重复10次。

mysqlslap -uroot -p --query="select count(*) from city" -c 100 -i 10 --create-schema=world_innodb

show variables like '%max%';

max_connections 151.

show global status like '%max%';

Max_used_connections | 1 曾经最大连接数。

(student guide)书开始:

--------------------

SELECT INET_NTOA(123123124124);转换为IP

SELECT NTOA_INET(123123124124);转换为数字

select conv(12312432143,10,2);10进制变2进制。

show create table rental\G

EXPLAIN SELECT CUSTOMER_id from rental where year(rental_date)=2006 and month(rental_date)=2\G;

EXPLAIN SELECT CUSTOMER_id from rental where rental_date between '2006-02-01' and '2006-02-28'\G;

EXPLAIN SELECT CUSTOMER_id from rental rental_date between '2006-02-01 00:00:00' andcast('2006-02-01 00:00:00' as DATETIME)-INTERVAL 1 SECOND \G.

CAST转换。转换为as 后面的DATETIME.

select cast(curdate() as DATETIME);

select str_to_date('07/13/2001','%m/%d/%y');

select cast('2013-11-21' as date);OK, select cast('11/21/2012' as date);这就不认识了。

select str_to_date('07/13/2001','%m/%d/%y');

select cast(str_to_date('07/13/2001','%m/%d/%y') as DATETIME);

select from_days(366);

select from_days(365);

select hour(now());

select time(now());

微秒Mysql没有该类型,最新版本才有。需要用DOUBLE模拟。p90页(student guide)。

timestamp,在oracle表示微秒?

在此处算UNIX_TIMESTAMP('2008-02-28 00:00:00')变成多少秒,从1970开始。

算第三周。

select '2008-02-01'+INTERVAL((2-DAYOFWEEK('2008-02-01') % 7) +21) DAY AS third_monday;

DAYOFWEEK是一周第几天,周日是第一天。

create table city8 like city;

alter table city8 engine=memory;

show processlist;

kill 3653;

select index_name,column_name,seq_in_index,

cardinality,index_type

from information_schema.statistics

where table_name='city8' and table_schema='sakila';

mysql> load data infile 'd:/city.txt' into table city(@skip,@Name,countrycode,@district,population)set name=concat(@name,' ',@district);

name设置,distirct没有设置,skip还会自动增长,但还是要占个位。加@的名称都随便起。

innodb_buffer_pool_size越大越好。内存快。

innodb_flush_log_at_trx_commit;

吞吐量上去,提交的数据可能没存盘,0,1,2;1提交一秒就存盘。与oracle一致。

默认逐行check。foreign_key_checks=0;关闭逐行,当全进来的时候开启。=1开启逐行。

insert批量缓冲设置。越大越好。

show variables like '%insert%';

bulk_insert_buffer_size | 8388608 |

如何算钱:e(log(1+ror))

NULLIF,相等NULL,不相等第一个

coalesce()返回第一个 不为NULL的表达式值。

中位值。P122页

存储过程:网络开销,服务器端共享。

RANK.P133

算税:建表,自关联。P141

round(12.233334,2)=12.12

rand(),随机数。放变量里赋值。set @n=rand()*1000;

create table tt(id int primary key,name varchar(10),score int);

insert into tt values(1,'s',97);

optimizer_prune_level | 1 是1好。??

|

optimizer_search_depth | 62 搜索深度。适度

select id,name,score,score as r_score,

(select count(distinct(score))+1 from tt

where score >r_score) as rank from tt order by rank; ?

优化器:

做join其实在内存中。

in 自动优化----》Or

链接:

exist,属于半连接。

反连接,取另一半。

嵌套循环链接:一行一行去链接。从最少行数表依次进行。

oralce的hint select /*+FULL(T)*/ from tt where id=100

mysql的straight_join

explain select country.name,city.name from country join city on country.code = city.countrycode\G;

explain select country.name,city.name from country straigth_join city on country.code = city.countrycode\G;??

show status like '%select %';自服务器启动以来总共做过多少次的选择。这就是状态变量。

show variables like '%insert%';这里参数是oracleDBA的管理员关注。

INTERSECT---->using(name,region)

minus ---->using(name,region) where name is null。

把有向图转换为表。节点、入口、出口。

树:节点、父节点。看不懂的语句在于Join的机制。

create table tree (node char(1) primary key not null,parent char(1));

insert into tree values('A',NULL),('B','A'),('C','A'),('D','A'),('E','B'),('F','B'),('G','C'),('H','C');

hierarchy,一个人被多个人管理。

select * from tree as t1 left join tree as t2 on t2.parent=t1.node

left join tree as t3 on t3.parent=t2.node;太不容易理解了。。自然智慧无限。

oracle有对应的查看树的语句:collection,可以看网络资料。

B+TREE实现的也是B-TREE.有一部分放不下就存在磁盘上。

B-TREE是逻辑叫法。

fulltext 是b-tree,B+树。

反转函数,Mysql搞不定,思路是新建立一个列然后用触发器方式把数据倒转放到该列,建立索引,然后再操作这个列就好。

%放后面好。

锁:

行锁只有Innodb才有。

innodb和Myisam.中:一致。

lock tables city read; 对方只能读,不能写。

lock tables city write; 不能读,不能写。

unlock tables ;一释放就都释放了。

help unlock;

myisam:

lock in update

concurrent_insert =1容许同时看和插入,条件是不能存在空洞。插入到最后一行。

=2,如果没人再查,则可以插入到空洞中。 请问此处与innodb的区别。

存储的机制如何都是B方式的吧。

索引、锁、存储。

innodb:

行锁。

防止幻行插入,for update:Next_key_lock.

select * from city where id>2 for update,

则别的事务插入id=3,则插入不了。

//with rollup汇总。就是所有行,各列相加。

物化视图:统计表,更新推荐job,而不是trigger.

序列,oracle不保证连续性,只保证唯一性。

在mysql中也是如此,用一个表只放一个列,用来记录序列。用一个存储过程来做实现该表的自增长。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: