博文

目前显示的是标签为“database”的博文

Understand the index in database tables/views/columns

Index的定义: An index is an on-disk structure associated with a table or view that speeds up retrieval of rows from the table or view. Index的类型可以有很多种: UNIQUE - A unique index is one in which no two rows are permitted to have the same index key value. CLUSTERED - Creates an index in which the logical order of the key values determines the physical order of the corresponding rows in a table. NONCLUSTERED - Creates an index that specifies the logical ordering of a table. With a nonclustered index, the physical order of the data rows is independent of their indexed order. 为了立即上面这些不同类型的Index首先我们要看一下table的存储方式。 "A heap is a table without a clustered index." 当一个table没有clustered index时, 这个table的rows就不会以sorted/ordered方式存储在disk.这就意味着,如果这个table连non-clustered的index也没有时,如果我们想要找出其中一个column的一个value, 它就需要遍历整个table了。 所以这就是clustered index的作用,同时也是为什么clustered index对每个table只能有一个。 那么什么是non-clustered的index呢? "Nonclustered indexes have a structure separate from the data rows. A no...

远程使用mysqldump以及正确理解ssh tunnel

在我的remote_server上安装有mysql服务,其使用3306端口。 使用mysqldump命令可以backup数据库到sql文件。比如: mysqldump -P 3310 -h 127.0.0.1 -u root -p database_name > dump_file.sql 这段命令的意思是,登陆到host(-h)127.0.0.1(本地?没错,通常我们使用mysqldump会登陆到远程mysql服务然后将备份文件dump到远程服务器。但是在这里,我们不想将文件放在远程服务器,我们想直接将文件传送回本地client。)并将mysqldump命令使用于127.0.0.1 的3310端口,进行数据的备份。 我们之所以可以这么做,是因为在执行这个命令之前我们使用了 ssh -f -L3310:localhost:3306 username@remote_server -N 这个命令来建立一个ssh 通道。这段命令的意思是,我从client来使用ssh建立到remot_server的连接,并且(-L)请将相对于remote_server来说的localhost以及localhost的3306端口映射到我client的3310端口。(-f 参数是要ssh连接后进入到后台,然后再client的命令行上就不会显示username@remote_server>) (-N 参数是说在ssh连接这时没有命令要执行了,即“只是用来forward port”) Aha, 这样当我们在client的3310端口上执行mysqldump命令时,我们就相当于在"相对于remote_server来说的localhost的3306端口", 其实就是remote_server的3306端口执行mysqldump命令。而且mysqldump会返回数据到client的3310端口。 这样我们就实现了直接将remote database的备份直接拷贝到client了。 关于更多ssh tunnel的讨论见: http://chamibuddhika.wordpress.com/2012/03/21/ssh-tunnelling-explained