Archive for 数据库

mysql导入数据

相信很多朋友都试过需要从老版本升级到新版本或新版本降到老版本的事。

我现在需要把mysql4.1的升级为Mysql5.1,用Mysqldump然后再mysql导入,怎样也出错。直接复制库文件也不行。

最后通过官方找到一个方法
先通过mysqldump导出

  1. shell> mkdir DUMPDIR
  2. shell>mysqldump --tab=DUMPDIR db_name

然后到目标机器运行

  1. shell> mysqladmin create db_name           # create database
  2. shell> cat DUMPDIR/*.sql | mysql db_name   # create tables in database
  3. shell> mysqlimport db_name DUMPDIR/*.txt   # load data into tables

这方法适用于旧版本迁移到新版本,同样适用于新版本降级

评论

MySQL复制表结构和数据SQL语句

1.复制表结构及数据到新表

CREATE TABLE 新表
  SELECT * FROM 旧表

2.只复制表结构到新表

CREATE TABLE 新表
  SELECT * FROM 旧表 WHERE 1=2

即:让WHERE条件不成立.

CREATE TABLE 新表
  LIKE 旧表 //tianshibao

3.复制旧表的数据到新表(假设两个表结构一样)

INSERT INTO 新表
  SELECT * FROM 旧表

4.复制旧表的数据到新表(假设两个表结构不一样)

INSERT INTO 新表(字段1,字段2,…….)
  SELECT 字段1,字段2,…… FROM 旧表

Comments (9)

replace table VS insert into table on depulicate

     看标题就知道,我接下来要干什么了。呵呵。。今天无意中发现使用Replace原来可以这么简单的。呵呵。先看下,我从网上找过来的资料。
Read the rest of this entry »

Comments (1)

[转]VC代码连接PostgreSql

      好。。在连接postgresql时,如果用ADO或是ODBC连接的话,资料就很多了,其实用代码也没多少。只是要注意的一些小细节会折磨人很久噢。。       下载postgresql就不说了,我用的是8.0版本。在安装时,如果是软件运行的目标机器,那么,就不用选择develop选项中的组件了,如果是开发的话,你就保证其中的“include”和“lib”文件夹的组件被选中(安装过程一定要注意,因为界面会弹出很多对话框的),如果你是第二次在同一台机器上安装多次的话,有可能会产生“the user have already existed”那么可以使用net user postgres /delete (如果选择的用户名是postgres的话);

Read the rest of this entry »

Comments (1)

[转]MySQL SQL Profiler性能分析器

申请了MySQL版主,说要15天的考察期
看来要好好表现了。不能再潜水了。

看到很多朋友问MySQL的性能分析器
因为MySQL在这方面做得比较差,也可能我不知道
没有Microsoft的SQL Profiler,也没有Oracle的Audit和AWR
所以我们很难得到一些SQL语句的统计,这也给我们调优带来了困难
更难的是对MySQL的追踪
以前写过个,不过在看过mysql网站上的一个bash脚本后,觉得自己的那个就是小巫见大巫了
现拿来分享给大家,很简单
# 取得网卡eth0上的所有操作

  1. time tcpdump -i rl0 -s 1500 -w 20060427-db-traffic-01.dmp 

# 这个可以指定特定IP请求的操作,可用于追踪
time tcpdump -i eth0 -s 1500 src host 192.168.2.10 -w 20060427-db-traffic-01.dmp 
# 这个是格式化输出你要结果

  1. strings 20060427-db-traffic-01.dmp | grep -i 'select' | awk '{printf("%s %s %s %s\n", $1,$2,$3, $4);}'| sort| uniq -c | awk '{printf("%06ld %s %s %s %s\n", $1,$2,$3,$4,$5);}'|sort

得到的结果:
cpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 1500 bytes
12000 packets captured
12000 packets received by filter
0 packets dropped by kernel
real 0m8.666s
user 0m0.006s
sys 0m0.016s
——–
000001 select last_insert_id() from system_parameter

000122 select count(1) from visit_tracking
000122 select visitor_id
000800 select web_page_id , web_page_type_id
000800 select web_page_type_id , name
003200 select count(1) from hit_count
006400 select pd.parameter_value,
006400 select rp.user_id , rp.update_time 

可以看到执行各种select的次数,当然改一下就可以看insert,update这种操作次数
配合log-slow-queries,你可以进一步的优化

Comments (1)

清除master logs

把多余的日志删除了。只保留当前一个日志文件。 

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 99 to server version: 5.0.24-community-nt-log
 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 

mysql> show master logs;
+------------------+------------+
| Log_name         | File_size  |
+------------------+------------+
| mysql-bin.000026 | 1073742051 |
| mysql-bin.000027 |  675531614 |
+------------------+------------+
2 rows in set (0.03 sec)
 

mysql> purge master logs to 'mysql-bin.000027';
Query OK, 0 rows affected (0.31 sec)       <------------- 这里 ---------
mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000027 | 675627704 |
+------------------+-----------+
1 row in set (0.00 sec)
 

mysql>

 

评论

pgsql 安装

cd /usr/ports/databases/postgresql82-server
make install clean

cd /usr/ports/databases/phppgadmin
make install clean

su pgsql
initdb -D /usr/local/pgsql/data

createuser -s -P 用户名

在/etc/rc.conf加入
postgresql_enable="YES"

启动:
/usr/local/etc/rc.d/010.pgsql.sh start

评论

super-smack

Super-smack 是一个强大的广受赞誉的压力测试工具,支持MySQL和PostgreSQL。这个工具程序现在由 Tony Bourke 维护。

Read the rest of this entry »

Comments (1)

MySQL Benchmarking 4: Compiling Super-Smack on Solaris 10

I hate to start the blog post with the same "I’ve been really busy excuse" but honestly that’s the truth. Ever since I wrote the last post, one important thing happened to me. My dentist prescribed me a mouth wash and it turned out that I was severely allergic to it. My upper lip became extremely swollen and remained like that for couple of days until I got a shot of steroid. Ughh, it was really painful. Anyways, time has just been flying by and I have a lot of catching up to do.

This is the part 4 of MySQL Benchmarking series. In the last post I showed you how you can compile Sysbench on Solaris 10. In this post, I will be showing you how you can compile Super-Smack on Solaris 10.

MySQL Super Smack was originally developed by Sasha Pachev (sasha at surveypro dot com), a former MySQL employee. After Sasha, Jeremy Zawodny of Yahoo! took over the maintenance of Super Smack. When Jeremey got really busy with Yahoo! stuff, Tony Bourke took over the Super Smack project.

Getting Super Smack to compile on Solaris 10 was really a headache even though a Sun blog post claimed it wasn’t. Nonetheless, I finally succeeded with the help of Andy.

Download Super Smack source (the current version is 1.3).

Read the rest of this entry »

评论

truncate 和delete的区别

注意:这里说的delete是指不带where子句的delete语句
相同点
truncate和不带where子句的delete, 以及drop都会删除表内的数据

不同点:
1. truncate和 delete只删除数据不删除表的结构(定义)
    drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger),索引(index); 依赖于该表的存储过程/函数将保留,但是变为invalid状态.
2.delete语句是dml,这个操作会放到rollback segement中,事务提交之后才生效;如果有相应的trigger,执行的时候将被触发.
   truncate,drop是ddl, 操作立即生效,原数据不放到rollback segment中,不能回滚. 操作不触发trigger.
3.delete语句不影响表所占用的extent, 高水线(high watermark)保持原位置不动
  显然drop语句将表所占用的空间全部释放
  truncate 语句缺省情况下见空间释放到 minextents个 extent,除非使用reuse storage;   truncate会将高水线复位(回到最开始).
4.速度,一般来说: drop>; truncate >; delete
5.安全性:小心使用drop 和truncate,尤其没有备份的时候.否则哭都来不及
使用上,想删除部分数据行用delete,注意带上where子句. 回滚段要足够大.
想删除表,当然用drop
想保留表而将所有数据删除. 如果和事务无关,用truncate即可. 如果和事务有关,或者想触发trigger,还是用delete.
如果是整理表内部的碎片,可以用truncate跟上reuse stroage,再重新导入/插入数据

评论