mirror of
https://github.com/MariaDB/server.git
synced 2026-01-06 05:22:24 +03:00
This commit includes the work done in collaboration with Hugo Wen from
Amazon:
MDEV-33408 Alter HNSW graph storage and fix memory leak
This commit changes the way HNSW graph information is stored in the
second table. Instead of storing connections as separate records, it now
stores neighbors for each node, leading to significant performance
improvements and storage savings.
Comparing with the previous approach, the insert speed is 5 times faster,
search speed improves by 23%, and storage usage is reduced by 73%, based
on ann-benchmark tests with random-xs-20-euclidean and
random-s-100-euclidean datasets.
Additionally, in previous code, vector objects were not released after
use, resulting in excessive memory consumption (over 20GB for building
the index with 90,000 records), preventing tests with large datasets.
Now ensure that vectors are released appropriately during the insert and
search functions. Note there are still some vectors that need to be
cleaned up after search query completion. Needs to be addressed in a
future commit.
All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
As well as the commit:
Introduce session variables to manage HNSW index parameters
Three variables:
hnsw_max_connection_per_layer
hnsw_ef_constructor
hnsw_ef_search
ann-benchmark tool is also updated to support these variables in commit
https://github.com/HugoWenTD/ann-benchmarks/commit/e09784e for branch
https://github.com/HugoWenTD/ann-benchmarks/tree/mariadb-configurable
All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
Co-authored-by: Hugo Wen <wenhug@amazon.com>
67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
/* Copyright (c) 2023, MariaDB
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
|
|
|
|
|
/**
|
|
@file
|
|
|
|
@brief
|
|
This file defines all vector functions
|
|
*/
|
|
|
|
#include <my_global.h>
|
|
#include "item.h"
|
|
#include "item_vectorfunc.h"
|
|
|
|
key_map Item_func_vec_distance::part_of_sortkey() const
|
|
{
|
|
key_map map(0);
|
|
if (Item_field *item= get_field_arg())
|
|
{
|
|
Field *f= item->field;
|
|
for (uint i= f->table->s->keys; i < f->table->s->total_keys; i++)
|
|
if (f->table->s->key_info[i].algorithm == HA_KEY_ALG_VECTOR &&
|
|
f->key_start.is_set(i))
|
|
map.set_bit(i);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
double Item_func_vec_distance::val_real()
|
|
{
|
|
String *r1= args[0]->val_str();
|
|
String *r2= args[1]->val_str();
|
|
null_value= !r1 || !r2 || r1->length() != r2->length() ||
|
|
r1->length() % sizeof(float);
|
|
if (null_value)
|
|
return 0;
|
|
float *v1= (float*)r1->ptr();
|
|
float *v2= (float*)r2->ptr();
|
|
return euclidean_vec_distance(v1, v2, (r1->length()) / sizeof(float));
|
|
}
|
|
|
|
double euclidean_vec_distance(float *v1, float *v2, size_t v_len)
|
|
{
|
|
float *p1= v1;
|
|
float *p2= v2;
|
|
double d= 0;
|
|
for (size_t i= 0; i < v_len; p1++, p2++, i++)
|
|
{
|
|
float dist= *p1 - *p2;
|
|
d+= dist * dist;
|
|
}
|
|
return sqrt(d);
|
|
}
|