Home > Database > Mysql Tutorial > body text

mysql enables rapid creation of indexes for millions of data

小云云
Release: 2018-03-27 15:17:06
Original
2658 people have browsed it

Test data A local table users has 100 million records. There is no index added to the corresponding field before creating this table, so you need to add an index to the table at this time. However, due to the large amount of data, the index addition may not be successful. After thinking of many ways, I finally succeeded in digging the hole.

Start preparations,

user table structure:

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
  `add_time` int(11) DEFAULT NULL,  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1000001 DEFAULT CHARSET=latin1;
Copy after login


An error occurred when trying to use into outfile to export data in navicat:

select * from users into outfile 'users.txt'; //users.txt文件在mysql的dat
Copy after login

As shown below:

I checked online that it is the permissions set by mysql. You can use

show variables like '%secure% ';Check what is the current value of secure-file-priv?

You can see that the secure-file-priv parameter is used to limit which specification LOAD DATA, SELECT ... OUTFILE, and LOAD_FILE() is passed to directory. When the value of secure_file_priv is null, it means that mysqld is restricted from importing and exporting. When the value of secure_file_priv is /tmp/, it means that the import and export of mysqld can only occur in the /tmp/ directory. When the value of secure_file_priv has no specific value, it means that There are no restrictions on the import | export of mysqld.

The exported data must be the specified path of this value before it can be exported. The default may be NULL (what I am looking at here is null ) means that export is prohibited, so you need to set it

Since I am using the my.ini file under the phpstudy, mysql installation path, set the path:

Note: Add double quotation marks to the path name ""
The error remains the same after restarting...

This parameter is not set in mysql.cnf, which means that this parameter is the default value null

So try to modify [mysqld] in mysql.cnf to add secure_file_priv =

The final result is as follows:

Restart mysql again

You can see users.txt Generated in the test database directory at the same level. . .
Then execute:

truncate  users;
Copy after login

创建索引:

create index name using BTREE on users(name);
Copy after login

索引的方式有:BTREE、RTREE、HASH、FULLTEXT、SPATIAL,这里不再赘述他们的区别,网上有很多关于这块的介绍啦

导入文件到相应表users。

load data infile 'users.txt' into table users;
Copy after login

此方式在write 锁住情况下,或对外服务暂停时使用,线上不可能直接这样玩了。

相关推荐:

MySQL创建索引和删除索引的方法

The above is the detailed content of mysql enables rapid creation of indexes for millions of data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template