Индексирование для быстрого поиска по базе данных

For galleries with the number of faces over 1,000,000, we recommend you to speed up search by using a fast index. To prepare the fast index, you will need the findface-tarantool-build-index utility from your distribution package. This utility is independent of the tntapi component and can be installed either on a localhost or on a remote host with access to Tarantool.

Для подготовки быстрого индекса выполните следующие действия:

  1. Установите утилиту findface-tarantool-build-index.

    sudo apt-get install findface-tarantool-build-index
    
  2. Create the fast index for your gallery (testgal in the case-study). First, connect to the Tarantool console.

    Примечание

    You have to repeat the fast index creation on each tntapi shard.

    tarantoolctl connect 127.0.0.1:33001
    
  3. Run prepare_preindex. Each element of the gallery will be moved from the linear space to preindex:

    127.0.0.1:33001> FindFace.Gallery.new("testgal"):prepare_preindex()
    ---
    ...
    
  4. Сохраните пространство preindex в файл, который будет использован для генерации индекса:

    127.0.0.1:33001> FindFace.Gallery.new("testgal"):save_preindex("/tmp/preindex.bin")
    ---
    ...
    
  5. Launch index generation with the findface-build-index utility (see --help for additional options). Depending on the number of elements, this process can take up to several hours and can be done on a separate, more powerful machine (for huge galleries we recommend c4.8xlarge amazon, for example spot-instance).

    sudo findface-build-index --input /tmp/preindex.bin --facen_size 320 --out /opt/ntech/var/lib/tarantool/default/index/testgal.idx
    
    0% 10 20 30 40 50 60 70 80 90 100%
    |----|----|----|----|----|----|----|----|----|----|
    **************************************************
    0% 10 20 30 40 50 60 70 80 90 100%
    |----|----|----|----|----|----|----|----|----|----|
    **************************************************
    
    [Benchmark] create_index took 29.994ms
    Index saved at /opt/ntech/var/lib/tarantool/default/index/testgal.idx
    
  6. Удалите файл preindex.bin.

    sudo rm /tmp/preindex.bin
    
  7. Включите быстрый индекс для галереи.

    Примечание

    If Tarantool works as a replica set, copy the index file (.idx) from the master instance to the same path on the replica before enabling the fast index for the master instance (:use_index).

    Совет

    Рекомендуется удалять все файлы индекса на реплике, кроме последнего, во избежание промежуточных обновлений индекса в случае сильного отставания реплики от мастера.

    127.0.0.1:33001> FindFace.Gallery.new("testgal"):preindex_to_index()
    ---
    ...
    127.0.0.1:33001> FindFace.Gallery.new("testgal"):use_index("/opt/ntech/var/lib/tarantool/default/index/testgal.idx")
    ---
    ...
    
  8. После включения быстрого индекса поиск по галерее должен стать значительно быстрее (в 70-100 раз). Информация об индексе остается в служебном пространстве Tarantool, поэтому когда вы перезапускаете Tarantool, индекс также подгружается.

    Предупреждение

    Не перемещайте файл индекса!