作者:matrix
发布时间:2020-08-31
分类:Python 零零星星
第一次drop超过GB的数据表,没想到竟然会执行的这么慢。尝试过TRUNCATE
和DROP
都不满意。
后来就直接找到数据库储存的文件来删除,这样比起使用sql语句操作会快得多,但也是危险操作,无法找回。
删除操作脚本
运行环境 python3.7,依赖pymysql
,根据自身情况配置变量mysql_data_dir,db_config,table_names,condition_save
fast_drop_table.py
#codeing=utf-8
"""
快速清空超大数据表 保留想要数据
"""
import pymysql
import os
mysql_data_dir = '/mnt/mysql_data/db_name/' #数据库文件所在路径
# 数据库连接配置
db_config = {'host': '127.0.0.1', 'port': 3306, 'user': 'user', 'password': 'password', 'db': 'db_name', 'charset': 'utf8'}
# 需要清空操作的数据表
table_names = [
"com_hhtjim_badata_trades_eos_this_quarter",
"com_hhtjim_badata_trades_eth_this_quarter",
]
# 数据表保留的查询条件
condition_save = "timestamp > '2020-02-20T00:00:00Z'"
# condition_save = False# 不保留
class Db:
'''
简单数据库连接操作类
'''
def __init__(self,**kwargs):
self.connection = pymysql.connect(**kwargs)
self.cursor = self.connection.cursor()
if __name__ == "__main__":
mysql = Db(**db_config)
for table_name in table_names:
os.link('{}{}.frm'.format(mysql_data_dir,table_name), '{}{}.frm.h'.format(mysql_data_dir,table_name))
os.link('{}{}.ibd'.format(mysql_data_dir,table_name), '{}{}.ibd.h'.format(mysql_data_dir,table_name))
mysql.cursor.execute('CREATE TABLE {0}_back like {0}'.format(table_name))
mysql.connection.commit()
if condition_save:
mysql.cursor.execute("INSERT INTO {0}_back SELECT * FROM {0} WHERE {1} ;".format(table_name,condition_save))
mysql.connection.commit()
mysql.cursor.execute("drop table {}".format(table_name))
mysql.connection.commit()
mysql.cursor.execute("alter table {0}_back rename to {0};".format(table_name))
mysql.connection.commit()
os.unlink('{}{}.frm.h'.format(mysql_data_dir,table_name))
os.unlink('{}{}.ibd.h'.format(mysql_data_dir,table_name))
print('succeed: {}'.format(table_name))
具体步骤
### 找到frm,ibd文件
根据数据库存储路径找到需要删除的表名的frm,ibd文件。
### 建立硬连接
$ ln mytable.ibd mytable.ibd.h
$ ln mytable.frm mytable.frm.h
### 备份表结构
CREATE TABLE mytable_back like mytable;
### 备份想要保留的数据
INSERT INTO mytable_back SELECT * FROM mytable WHERE timestamp > '2020-02-27T00:00:00Z' ;
### 删除旧表
drop table mytable;
### 修改备份表名字
alter table mytable_back rename to mytable;
### 删除硬连接
$ rm -f mytable.frm.h mytable.ibd.h
参考:
https://blog.csdn.net/weixin_34034261/article/details/86250223
作者:matrix
发布时间:2019-07-15
分类:零零星星
本来只是想获取字符串的MD5hash值,结果发现麻烦。首先去mathworks找到的DataHash,GetMD5这些函数要么无法使用,要么就是计算出来的md5值是错误的,反正达不到效果。
刚开始以为mathworks的代码必须登录账户才能下载,现在才发现,点击Overview旁边的Functions
选项卡就能看到源代码。 😂
当初就算是找到了一些函数也是不能使用,最后还是使用python库来解决。好在matlab对python的调用非常简单。直接使用py
库就好了,唯一的问题就是有些python的语法和matlab不同,比如切片[:::]
操作这些。
安装python3环境
2019-08-19测试发现macos中3.7版本可以,但是在centOS中就调用失败。所以就centOS中测试安装3.5的版本了。
后面发现应该是编译时的enable-shared参数问题。
2020-02-18centos中测试可以直接用conda来安装python3.5使用,3.6,3.7测试调用失败!
开始手动编译安装
cd ~
yum install zlib-devel bzip2-devel openssl-devel ncurese-devel
wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
tar xf Python-3.5.0.tgz
cd Python-3.5.0
./configure --enable-shared #matlab要求必须设置enable-shared参数
make && make install
参考:
https://ww2.mathworks.cn/help/matlab/matlab_external/undefined-variable-py-or-function-py-command.html#buialof-67
https://ww2.mathworks.cn/help/matlab/getting-started-with-python.html?lang=en
切换默认的python解释器
matlab输入
pyversion`可以查看python解释器路径,如果需要修改:
pyversion /usr/local/bin/python3 %切换到python3.7指定路径
直接调用python内置函数
计算字符串md5函数
使用python的hashlib库进行操作
function[re]=md5(str)
%{
依赖python进行md5 hash
%}
m2 = py.hashlib.md5();
m2.update(py.str(str).encode('utf-8'));
re = char(m2.hexdigest());
说明:
此函数依赖python环境,确保系统中有安装。
兼容Linux,Mac,Windows
最后char函数是将py.str
类型转换为matlab的str
知道了这个操作,以后很多操作就会非常简单。
比如
获取当前时间戳:
str2num(char(py.int(py.time.time())))
直接用py
库来调用即可,最后再使用matlab函数str2num,char处理成number类型。
调用自定义.py脚本函数
上面的方法倒是可以解决一部分难题,但是遇到方括号[]、百分号%操作符这些直接在.m
文件中就不会执行了。毕竟matlab是以它的语法来解析,这样只得起调.py
文件来操作。
新建test.py
文件位于当前工作目录
def func():
return '123'[::-1]
载入python脚本:
%脚本所在路径,空字符串表示当前目录
path = '' %当前目录
%path = 'a/' %当前的子目录a
if count(py.sys.path,path) == 0
insert(py.sys.path,int32(0),path);
end
%清除脚本缓存,执行重载操作
clear classes;
mod = py.importlib.import_module('test');%载入的脚本模块
%mod = py.importlib.import_module('a.test');%载入的子目录下的脚本模块
py.importlib.reload(mod);
调用py.test.func
即可得到切片结果321
,注意为py.str
类型。
子目录形同:py.子目录.文件名.函数名
当修改py文件之后再调用会发现还是之前的结果,matlab缓存了之前的python脚本,所以这里需要clear classes...
这类操作。
参考:
MATLAB 调用 Python
https://www.cnblogs.com/zknublx/p/6212590.html
https://blog.csdn.net/jnulzl/article/details/51170859
https://zhuanlan.zhihu.com/p/42598340
https://blog.csdn.net/x5675602/article/details/52134981
http://www.1cae.com/a/MatLab/52/python-matlab-8943.htm
作者:matrix
发布时间:2019-03-12
分类:Python
虚拟机ubuntu中自带了Python2,但是想要使用python3需要单独安装。使用add-apt-repository
来添加ppa源安装提示command not found
,之后进行安装操作sudo apt-get install software-properties-common
,错误继续:
$ sudo apt-get install software-properties-common
Reading package lists... Done
Building dependency tree... 50%
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
libmysql++-dev : Depends: libmysqlclient-dev but it is not going to be installed
software-properties-common : Depends: python3:any (>= 3.3.2-2~)
Depends: python3 but it is not going to be installed
Depends: python3-gi but it is not going to be installed
Depends: gir1.2-glib-2.0 but it is not going to be installed
Depends: python-apt-common (>= 0.9) but it is not going to be installed
Depends: python3-dbus but it is not going to be installed
Depends: python3-software-properties (= 0.96.20.8) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
继续apt-get update & upgrade & 更换国内源,后面使用apt-get install Python3也是相同错误,无济于事。
错误跟踪到libmysql++-dev依赖问题,遂重新安装sudo apt-get install libmysqlclient-dev
:
$ sudo apt-get install libmysqlclient-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
libmysqlclient-dev
0 upgraded, 1 newly installed, 0 to remove and 163 not upgraded.
4 not fully installed or removed.
Need to get 0 B/1,167 kB of archives.
After this operation, 7,040 kB of additional disk space will be used.
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 51891 files and directories currently installed.)
Preparing to unpack .../libmysqlclient-dev_5.7.25-0ubuntu0.16.04.2_amd64.deb ...
Unpacking libmysqlclient-dev (5.7.25-0ubuntu0.16.04.2) ...
dpkg: error processing archive /var/cache/apt/archives/libmysqlclient-dev_5.7.25-0ubuntu0.16.04.2_amd64.deb (--unpack):
unable to install new version of '/usr/include/mysql/mysql/plugin_ftparser.h': No such file or directory
Errors were encountered while processing:
/var/cache/apt/archives/libmysqlclient-dev_5.7.25-0ubuntu0.16.04.2_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
尝试手动安装deb:sudo dpkg -i /var/cache/apt/archives/libmysqlclient-dev_5.7.25-0ubuntu0.16.04.2_amd64.deb
失败:
unable to install new version of '/usr/include/mysql/mysql/plugin_ftparser.h': No such file or directory
根源问题
/usr/include/mysql/mysql/plugin_ftparser.h
不存在
开始手动查找
$ sudo find / | grep plugin_ftparser.h
该文件只存在与/usr/local/mysql/include/
目录中,ls /usr/include/mysql -al
查看列表就会发现mysql
的软链接出现问题导致的。
解决
重新生成mysql include软连接
$ ln -s /usr/local/mysql/include /usr/include/mysql/mysql #重新生成软连接
此虚拟机中的mysql是之前安装LNMP搭建PHP环境而创建的,很有可能和这个有关系。
参考:
https://askubuntu.com/questions/629448/file-mysql-h-cant-be-found
https://askubuntu.com/questions/773079/fixing-broken-packages-in-ubuntu-repost
PEACE~