mysql and sqlalchemy

来源:互联网 发布:linux 监听端口命令 编辑:程序博客网 时间:2024/06/05 07:23

索引

建立索引的时候会自动暗地里为这个索引的子集创建索引。

比如INDEX index1 (id,name),

        UNIQUE INDEX index2 (name),

其中。先为id、name都创建了索引。即使你没有为id创建索引,实际上mysql已经为他创了,但除非显式创建,否则id本身的索引是没有被创建的。


mysql中的全文检索

通过一个特殊的索引(FULLTEXT索引)引入了对文本字段中的文本元素进行搜索。

INSERT INTO Document(url,page_text) VALUES ('index.html',''The page contents.');

SELECT url FROM Document WHERE MATCH(page_text) AGAINST ('page');搜寻page_text中包含有page这个词的记录,并选出这个记录的url来。

文件服务

从user表中export出数据,每个数据用,(逗号)分割。
mysql> select * into outfile 'c://user.xls' fields terminated by ',' from user;
Query OK, 25 rows affected (0.00 sec)

excel中每个格子占一个数据。不用什么分割了
mysql> select * into outfile 'c://book.xls' from user;
Query OK, 25 rows affected (0.00 sec)


导入数据的话,

load data infile '/var/lib/mysql/test/identify.out' into table `table_name` fields terminated by ' ' lines terminated by '\n';

被导入的文件要放在数据库存放数据的目录中(权限的问题),如果是ubuntu的apt-get安装的话,目录在/var/lib/mysql/database_name/you_file_name.

fields terminated by 数据行之间的分隔符,我这里是空格, 

lines terminated by '\n' 行的分隔符。有些你可能要用'\r\n'

如果是远程的文件,则可以用mysqlimport 



备份数据库

 mysqldump --opt -d kw3_gamearticle -h "127.0.0.1" -P 3306 -u root -p >kw3_gamearticle.sql

去掉--opt -d 则连数据也导出


日志

配置mysql输出日志。


加在mysql.ini中。
general_log  = 1
general_log_file = c:\mylog.log

在mysql.ini中,有---

# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit=1

此值为1时,mysql会在每次commit的时候把所有的ACID操作都刷到log中。0时,大约1秒钟才刷一次。2时,每次commit时都会写入log,但只是一秒后才刷到真正的磁盘中。

SHOW TABLE STATUS FROM `COLLECT`  like 'UC_TABLE_24'
<NameEngineVersionRow_formatRowsAvg_row_lengthData_lengthMax_data_lengthIndex_lengthData_freeAuto_incrementCreate_timeUpdate_timeCheck_timeCollationChecksumCreate_optionsCommentUC_TABLE_24MyISAM10Dynamic128710442.81475E+1420480202013-4-2 11:562013-4-2 14:10NULLutf8_general_ciNULL  




sqlalchemy

我用t=sqlalchemy.Table(t_name,metadata,autoload=True)取得已建的表,比如存在t.c.mfield。删掉mfield后,t2=sqlalchemy.Table(t_name,metadata,autoload=True,extend_existing=True)发现t2.c.mfield还是存在???

一个不好的办法便是,再执行一次metadata=sqlalchemy.MetaData(engine)。然后t2=。。。

如果我是新增字段newfield,然后t2=...,t2.c.newfield存在。


 select column_comment from information_schema.columns where table_name='user' and column_name='user_id' and table_schema='test'

 由于sae的mysql不能用长连接。所以每次web中的request请求结束之后都需要remove,最好用create_session=ScopedSession( sqlalchemy.orm.sessionmaker())

我是用了bottle-sqlalchemy插件,能自动remove。其中from sqlalchemy.orm.scoping import ScopedSession
engine=sqlalchemy.create_engine('mysql://%s:%s@%s:%s/app_gausszh' % (sae.const.MYSQL_USER, sae.const.MYSQL_PASS,sae.const.MYSQL_HOST,sae.const.MYSQL_PORT),echo=True, echo_pool=True,pool_size=30, convert_unicode=True,encoding='UTF-8',pool_recycle=10)


sqlalchemy 中也许你需要水平分表,但是总不可能手写十几个Class吧,嗯,这个时候就需要type

type它也能动态的创建类。type可以接受一个类的描述作为参数,然后返回一个类。参考http://blog.csdn.net/gausszh/article/details/8035939

for i in 分表的个数:    example_class = type('example_class_%s' % i,(Base,),{       '__tablename__':'example_class_table_%s'% i,       'id':Column(Integer),       'field_name':Column(String(32),index = True)       }    )





原创粉丝点击