【问题标题】:Best way to index very long filepaths in mysql在mysql中索引非常长的文件路径的最佳方法
【发布时间】:2013-01-24 17:39:06
【问题描述】:

给定以下文件路径,存储在 mysql 数据库中:

.//.git/refs/remotes/origin/HEAD
.//.git/refs/tags
.//__init__.py
.//__init__.pyc
.//forms.py
.//forms.pyc
.//models.py
.//models.pyc
.//settings.py
.//settings.pyc
.//static
.//static/css
.//static/css/all.css
.//static/images
.//static/images/bg.png
.//static/images/favicon.ico
.//static/images/pds-header-logo.png
.//static/images/pds-logo.png
.//static/images/revolver.png
.//static/js
.//static/js/all.js
.//templates
.//templates/base.html
.//templates/default.html
.//templates/overview.html
.//urls.py
.//urls.pyc
.//views.py
.//views.pyc
.//wsgi.py
.//wsgi.pyc

需要有人能够搜索路径。例如,如果用户搜索“static”,它将返回路径中带有“static”的结果:

.//static
.//static/css
.//static/css/all.css
.//static/images
.//static/images/bg.png
.//static/images/favicon.ico
.//static/images/pds-header-logo.png
.//static/images/pds-logo.png
.//static/images/revolver.png
.//static/js
.//static/js/all.js

我目前的搜索是这样的:

`SELECT path FROM files WHERE path LIKE '%search%';`

有没有办法索引这个列/改进这个搜索(删除 LIKE %%),因为我在这个系统上可能有 1M+ 文件路径。请注意,文件路径可能超过 200 个字符。

【问题讨论】:

    标签: mysql linux unix database-design


    【解决方案1】:

    你不能。通配符搜索不会使用索引。
    如果你索引文件路径,你只能最好地支持像

    这样的查询
    /static/images/%
    

    根据您的情况,如果您想允许通配符搜索,
    你最好的办法是把目录炸成多个关键字:

    static
    images
    revolver
    .png
    

    然后将每个关键字存储到关键字表中,
    并建立关系。

    当你执行通配符搜索时,实际上是搜索关键字表。

    【讨论】:

    • 这种方法的好主意。我会尝试非索引,如果太慢,我会使用你的拆分/关键字方法。
    【解决方案2】:

    我猜你实际上可以有一个“部分名称的索引”。像这样的:

    id ! name    ! parent
    ---------------------
    1  ! static  ! 0           // at root. 
    2  ! css     ! 1           // Parent is "static"
    3  ! all.css ! 2           // parent is css
    4  ! images  ! 1           // parent is static
    5  ! bg.png  ! 4           // images. 
    

    读取原始文件名需要一些工作,除非您也存储它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多