Mysql性能:如何在数据库中存储文件树?

发布于 2024-11-03 16:33:33 字数 1735 浏览 1 评论 0原文

我正在尝试将文件存储在数据库中并查询文件夹的数量和大小。我的表如下。

CREATE TABLE `files` (
`permission` varchar(10) DEFAULT NULL,
`size` int(11) NOT NULL DEFAULT '0' COMMENT 'filesize',
`date` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`time` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`path` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
KEY `path` (`path`(10)),
KEY `path_2` (`path`(4)),
KEY `size` (`size`)
) ENGINE=MyISAM

我的文件夹就像“???/一些字符串”。我大约有 38,333,316 条记录。 简单的查询大约需要22秒。

mysql> select count(*),sum(size)/(1024.0*1024.0*1024.0) from files 
where path like '5___%' and size != 2048;
+----------+----------------------------------+
| count(*) | sum(size)/(1024.0*1024.0*1024.0) |
+----------+----------------------------------+
|  7900746 |                        9019.6535 |
+----------+----------------------------------+
1 row in set (22.89 sec)

mysql> explain select count(*),sum(size)/(1024.0*1024.0*1024.0) 
       from files where path like '5___%' and size != 2048;
--+-----------+-----+----+-------------- -+----+-------+----+--------+-----------
id|select_type|table|type|possible_keys   |key |key_len|ref |rows    | Extra     
--+-----------+-----+----+----------------+----+-------+----+--------+-----------
 1| SIMPLE    |files| ALL|path,path_2,size|NULL|NULL   |NULL|38333316|Using where
--+-----------+-----+----+----------------+----+-------+----+--------+-----------
1 row in set (0.23 sec)

有没有办法优化查询,我可以以某种方式加快速度吗? 通常我会问一些类似的问题:

path like '5___%' and size != 2048
path like '3___%' and size != 2048
path like '45__%' and size != 2048

谢谢阿曼。

i am trying to store the files in the database and query the count and size of the folders. My Table is following.

CREATE TABLE `files` (
`permission` varchar(10) DEFAULT NULL,
`size` int(11) NOT NULL DEFAULT '0' COMMENT 'filesize',
`date` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`time` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
`path` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
KEY `path` (`path`(10)),
KEY `path_2` (`path`(4)),
KEY `size` (`size`)
) ENGINE=MyISAM

my folders are like "????/some string". I have about 38,333,316 records.
The simple query takes about 22 seconds.

mysql> select count(*),sum(size)/(1024.0*1024.0*1024.0) from files 
where path like '5___%' and size != 2048;
+----------+----------------------------------+
| count(*) | sum(size)/(1024.0*1024.0*1024.0) |
+----------+----------------------------------+
|  7900746 |                        9019.6535 |
+----------+----------------------------------+
1 row in set (22.89 sec)

mysql> explain select count(*),sum(size)/(1024.0*1024.0*1024.0) 
       from files where path like '5___%' and size != 2048;
--+-----------+-----+----+-------------- -+----+-------+----+--------+-----------
id|select_type|table|type|possible_keys   |key |key_len|ref |rows    | Extra     
--+-----------+-----+----+----------------+----+-------+----+--------+-----------
 1| SIMPLE    |files| ALL|path,path_2,size|NULL|NULL   |NULL|38333316|Using where
--+-----------+-----+----+----------------+----+-------+----+--------+-----------
1 row in set (0.23 sec)

Are there way to optimize the queries, can I speed up somehow?
Usually I expect to ask some questions similar to this:

path like '5___%' and size != 2048
path like '3___%' and size != 2048
path like '45__%' and size != 2048

Thanks Arman.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2024-11-10 16:33:33

尝试这样的事情:

CREATE TABLE `directories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `path` varchar(1024) NOT NULL,
  `permissions` varchar(10) NOT NULL,
  `timestamp` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `path` (`path`(4))
) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TABLE `files` (
  `directory_id` int(10) unsigned NOT NULL,
  `filename` varchar(1024) NOT NULL,
  `permissions` varchar(10) NOT NULL,
  `timestamp` datetime NOT NULL,
  `size` bigint(20) unsigned NOT NULL,
  KEY `size` (`size`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

SELECT COUNT(*), SUM(size)/1073741824 FROM files WHERE directory_id IN(SELECT group_concat(id SEPARATOR ',') FROM directories WHERE path LIKE '5___%');

Try something like this:

CREATE TABLE `directories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `path` varchar(1024) NOT NULL,
  `permissions` varchar(10) NOT NULL,
  `timestamp` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `path` (`path`(4))
) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TABLE `files` (
  `directory_id` int(10) unsigned NOT NULL,
  `filename` varchar(1024) NOT NULL,
  `permissions` varchar(10) NOT NULL,
  `timestamp` datetime NOT NULL,
  `size` bigint(20) unsigned NOT NULL,
  KEY `size` (`size`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

SELECT COUNT(*), SUM(size)/1073741824 FROM files WHERE directory_id IN(SELECT group_concat(id SEPARATOR ',') FROM directories WHERE path LIKE '5___%');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文