Download the PHP package maxguru/mysql-vector without Composer

On this page you can find all versions of the php package maxguru/mysql-vector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package mysql-vector

A Library for MySQL Vector Operations

Overview

The VectorTable class is a PHP implementation designed to facilitate the storage and search of high-dimensional vectors in a MySQL database. This class stores normalized vectors as binary float arrays (VARBINARY) and quantized binary codes (VARBINARY), and uses a two-stage search algorithm for efficient similarity search.

Features

Requirements

Search Performance

This library is suitable for datasets up to 1,000,000 vectors. For larger datasets, consider using a dedicated vector database such as Qdrant.

Search Benchmarks (384-dimensional vectors, MySQL 8.0.40): Vectors Time (seconds)
100 0.0036
1000 0.0048
10000 0.0104
100000 0.0606
1000000 1.3893

Search Performance Degradation on MariaDB and MySQL 5.7

MariaDB's native bitwise XOR operator (^) (as well as one in MySQL 5.7) is limited to 64-bit integers. Unlike MySQL 8.0+ which added support for bit operations on binary string types, MariaDB and MySQL 5.7 still implicitly casts operands to a BIGINT for bitwise operations. Additionally, BIT_COUNT(expr) has similar issues on MariaDB and MySQL 5.7. This means that binary code longer than 64 bits is truncated and the Hamming distance is incorrect. We implement a workaround (chunked popcount fallback) in the library to correctly compute the Hamming distance on MariaDB and MySQL 5.7, however, it has a significant performance overhead.

Search Benchmarks (384-dimensional vectors): Vectors MySQL 5.7.42 MariaDB 10.2.44
100 0.0037 0.0040
1000 0.0060 0.0077
10000 0.0335 0.0349
100000 0.2891 0.3050
1000000 3.3692 3.6693

Implementation Details

Vector Search

Vectors are binary quantized upon insertion into the database to optimize search speed and reranked to improve accuracy using a two-stage algorithm:

  1. Fast filtering using Hamming distance on binary quantized codes
  2. Precise re-ranking using cosine similarity (dot product) on normalized vectors

Computational Efficiency

The library stores only normalized vectors in the database, which provides computational efficiency, eliminating normalization overhead (cosine similarity is simply a dot product of normalized vectors).

Storage Efficiency

Normalized vectors are stored as 32-bit IEEE-754 floats (float32) in little-endian order inside the normalized_vector VARBINARY column. Round-trip encoding/decoding to and from binary can introduce very small precision differences compared to 64-bit doubles; typical tolerances are around 1e-6 when comparing vectors after storage/retrieval. This precision is sufficient for cosine-similarity/dot-product ranking in typical embedding-based applications and allows significantly smaller storage and better performance than 64-bit doubles. Storing 4 bytes per dimension (instead of 8 bytes) allows 2x higher limit on dimensions and halves storage and network costs, which improves performance for insert, read, and re-ranking, while still maintaining sufficient accuracy for similarity search purposes.

Binary quantized codes are stored in the binary_code VARBINARY column. Storage requirements are 1 bit per dimension, so a 384-dimensional vector requires 48 bytes.

High Dimension Support

Normalized vector storage uses VARBINARY(4 * dimension) and binary quantized codes use VARBINARY(ceil(dimension/8)). InnoDB enforces a 65,535‑byte maximum row size for the clustered record; long JSON values are stored off‑page but a fixed in‑row pointer remains. Our exact in‑row accounting with ROW_FORMAT=DYNAMIC is:

Inequality: (4d + 2) + (ceil(d/8) + 2) + 4 + 20 + 19 ≤ 65535 → 4d + ceil(d/8) + 47 ≤ 65535. The largest d satisfying this is 15,875, giving 4*15,875 + ceil(15,875/8) = 63,500 + 1,985 = 65,485 and total 65,485 + 47 = 65,532 (3‑byte headroom). Therefore the safe maximum dimension is 15,875.

Notes and references:

While the binary code could theoretically scale to 524,280 bits, the effective limit is governed by the total row size with normalized_vector + binary_code.

Installation

  1. Ensure that PHP and MySQL are installed and properly configured on your system.
  2. Install the library using Composer.

Usage

Initializing the Vector Table

Import the VectorTable class and create a new instance using the MySQLi connection, table name, and vector dimension.

Setting Up the Vector Table in MySQL

The library provides flexible initialization options for different use cases:

Initialization will throw an exception if the target table already exists. Ensure you use a unique base table name or clean up with deinitialize() before re-initializing.

Complete Initialization

The initialize method creates the vector table in the database:

Initialization with typed metadata indexes (optional)

In order to use the metadata filtering capabilities efficiently, indexes on specific JSON paths need to be created. Typed generated columns and their indexes are created by passing a map of JSON paths to SQL types:

The table schema includes:

Cleanup and Deinitialization

The library provides comprehensive cleanup capabilities:

Inserting and Managing Vectors

Calculating Cosine Similarity

Searching for Similar Vectors

Perform a search for vectors similar to a given vector using the two-stage cosine similarity algorithm. The topN parameter specifies the maximum number of similar vectors to return.

The library uses adaptive candidate selection for Stage‑1 (Hamming distance) filtering. By default, the candidate pool size is chosen based on the requested topN via:

This favors accuracy for small result sets (larger candidate pools) and efficiency for larger result sets (smaller multipliers). You can override this behavior by providing a custom multiplier.

Additional Operations

The library provides several methods for counting, selecting, and iterating over vectors:

The $orderBy parameter accepts an associative array mapping column names or JSON paths to sort direction ('ASC' or 'DESC'). Use 'id' to order by primary key, or JSON paths like '$.created_at' to order by metadata fields. If a generated column index exists for a JSON path, it will be used for efficient sorting; otherwise, the library falls back to JSON_EXTRACT.

Warning: Breaking changes in 3.x

This fork of the library (maxguru/mysql-vector) has breaking changes compared to the original 2.x version (allanpichardo/mysql-vector). Please review the relevant sections before upgrading.

2.0.x → 3.0.0

3.0.0 → 3.1.0

3.1.0 → 3.2.0

Contributions

Contributions to this project are welcome. Please ensure that your code adheres to the existing coding standards and includes appropriate tests.

Development

This project uses DDEV, a Docker-based development environment. To get started, install DDEV and run the following commands:

To run the tests, use the following command:

PHP CompatInfo (compatibility analysis)

Use PHP CompatInfo to run an analysis of the library:

If you need to rebuild the php-compatinfo database, use:

Roadmap

The following features are planned for future releases:

1) MariaDB 11.7 VECTOR INDEX Support

2) Schema Migration System

3) Enhanced Metadata Filtering

Sponsors

If you find this library useful and would like to see the roadmap delivered faster, please consider sponsoring the developers of the library. Your support is greatly appreciated.

Alex R.:

License

MIT License


All versions of mysql-vector with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2
ext-mysqli Version *
ext-json Version *
ext-ctype Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package maxguru/mysql-vector contains the following files

Loading the files please wait ...