mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-03 19:17:11 -05:00 
			
		
		
		
	fix(server): remove unnecessary guc settings for vector search (#14237)
remove unnecessary guc settings
This commit is contained in:
		
							parent
							
								
									f8bbc6eabe
								
							
						
					
					
						commit
						34fae31fd4
					
				@ -279,13 +279,7 @@ LIMIT
 | 
				
			|||||||
-- SearchRepository.searchSmart
 | 
					-- SearchRepository.searchSmart
 | 
				
			||||||
START TRANSACTION
 | 
					START TRANSACTION
 | 
				
			||||||
SET
 | 
					SET
 | 
				
			||||||
  LOCAL vectors.enable_prefilter = on;
 | 
					  LOCAL vectors.hnsw_ef_search = 200;
 | 
				
			||||||
 | 
					 | 
				
			||||||
SET
 | 
					 | 
				
			||||||
  LOCAL vectors.search_mode = vbase;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SET
 | 
					 | 
				
			||||||
  LOCAL vectors.hnsw_ef_search = 100;
 | 
					 | 
				
			||||||
SELECT
 | 
					SELECT
 | 
				
			||||||
  "asset"."id" AS "asset_id",
 | 
					  "asset"."id" AS "asset_id",
 | 
				
			||||||
  "asset"."deviceAssetId" AS "asset_deviceAssetId",
 | 
					  "asset"."deviceAssetId" AS "asset_deviceAssetId",
 | 
				
			||||||
@ -369,7 +363,7 @@ WHERE
 | 
				
			|||||||
ORDER BY
 | 
					ORDER BY
 | 
				
			||||||
  "search"."embedding" <= > $6 ASC
 | 
					  "search"."embedding" <= > $6 ASC
 | 
				
			||||||
LIMIT
 | 
					LIMIT
 | 
				
			||||||
  101
 | 
					  201
 | 
				
			||||||
COMMIT
 | 
					COMMIT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- SearchRepository.searchDuplicates
 | 
					-- SearchRepository.searchDuplicates
 | 
				
			||||||
@ -404,12 +398,6 @@ WHERE
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
-- SearchRepository.searchFaces
 | 
					-- SearchRepository.searchFaces
 | 
				
			||||||
START TRANSACTION
 | 
					START TRANSACTION
 | 
				
			||||||
SET
 | 
					 | 
				
			||||||
  LOCAL vectors.enable_prefilter = on;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SET
 | 
					 | 
				
			||||||
  LOCAL vectors.search_mode = vbase;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SET
 | 
					SET
 | 
				
			||||||
  LOCAL vectors.hnsw_ef_search = 100;
 | 
					  LOCAL vectors.hnsw_ef_search = 100;
 | 
				
			||||||
WITH
 | 
					WITH
 | 
				
			||||||
@ -436,7 +424,7 @@ WITH
 | 
				
			|||||||
    ORDER BY
 | 
					    ORDER BY
 | 
				
			||||||
      "search"."embedding" <= > $1 ASC
 | 
					      "search"."embedding" <= > $1 ASC
 | 
				
			||||||
    LIMIT
 | 
					    LIMIT
 | 
				
			||||||
      100
 | 
					      64
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
SELECT
 | 
					SELECT
 | 
				
			||||||
  res.*
 | 
					  res.*
 | 
				
			||||||
 | 
				
			|||||||
@ -111,7 +111,7 @@ export class SearchRepository implements ISearchRepository {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  @GenerateSql({
 | 
					  @GenerateSql({
 | 
				
			||||||
    params: [
 | 
					    params: [
 | 
				
			||||||
      { page: 1, size: 100 },
 | 
					      { page: 1, size: 200 },
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        takenAfter: DummyValue.DATE,
 | 
					        takenAfter: DummyValue.DATE,
 | 
				
			||||||
        embedding: Array.from({ length: 512 }, Math.random),
 | 
					        embedding: Array.from({ length: 512 }, Math.random),
 | 
				
			||||||
@ -137,7 +137,10 @@ export class SearchRepository implements ISearchRepository {
 | 
				
			|||||||
        .orderBy('search.embedding <=> :embedding')
 | 
					        .orderBy('search.embedding <=> :embedding')
 | 
				
			||||||
        .setParameters({ userIds, embedding: asVector(embedding) });
 | 
					        .setParameters({ userIds, embedding: asVector(embedding) });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await manager.query(this.getRuntimeConfig(pagination.size));
 | 
					      const runtimeConfig = this.getRuntimeConfig(pagination.size);
 | 
				
			||||||
 | 
					      if (runtimeConfig) {
 | 
				
			||||||
 | 
					        await manager.query(runtimeConfig);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      results = await paginatedBuilder<AssetEntity>(builder, {
 | 
					      results = await paginatedBuilder<AssetEntity>(builder, {
 | 
				
			||||||
        mode: PaginationMode.LIMIT_OFFSET,
 | 
					        mode: PaginationMode.LIMIT_OFFSET,
 | 
				
			||||||
        skip: (pagination.page - 1) * pagination.size,
 | 
					        skip: (pagination.page - 1) * pagination.size,
 | 
				
			||||||
@ -196,7 +199,7 @@ export class SearchRepository implements ISearchRepository {
 | 
				
			|||||||
      {
 | 
					      {
 | 
				
			||||||
        userIds: [DummyValue.UUID],
 | 
					        userIds: [DummyValue.UUID],
 | 
				
			||||||
        embedding: Array.from({ length: 512 }, Math.random),
 | 
					        embedding: Array.from({ length: 512 }, Math.random),
 | 
				
			||||||
        numResults: 100,
 | 
					        numResults: 10,
 | 
				
			||||||
        maxDistance: 0.6,
 | 
					        maxDistance: 0.6,
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
@ -236,7 +239,10 @@ export class SearchRepository implements ISearchRepository {
 | 
				
			|||||||
        cte.addSelect(`faces.${col}`, col);
 | 
					        cte.addSelect(`faces.${col}`, col);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await manager.query(this.getRuntimeConfig(numResults));
 | 
					      const runtimeConfig = this.getRuntimeConfig(numResults);
 | 
				
			||||||
 | 
					      if (runtimeConfig) {
 | 
				
			||||||
 | 
					        await manager.query(runtimeConfig);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      results = await manager
 | 
					      results = await manager
 | 
				
			||||||
        .createQueryBuilder()
 | 
					        .createQueryBuilder()
 | 
				
			||||||
        .select('res.*')
 | 
					        .select('res.*')
 | 
				
			||||||
@ -421,17 +427,14 @@ export class SearchRepository implements ISearchRepository {
 | 
				
			|||||||
    return results.map(({ model }) => model).filter((item) => item !== '');
 | 
					    return results.map(({ model }) => model).filter((item) => item !== '');
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private getRuntimeConfig(numResults?: number): string {
 | 
					  private getRuntimeConfig(numResults?: number): string | undefined {
 | 
				
			||||||
    if (this.vectorExtension === DatabaseExtension.VECTOR) {
 | 
					    if (this.vectorExtension === DatabaseExtension.VECTOR) {
 | 
				
			||||||
      return 'SET LOCAL hnsw.ef_search = 1000;'; // mitigate post-filter recall
 | 
					      return 'SET LOCAL hnsw.ef_search = 1000;'; // mitigate post-filter recall
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let runtimeConfig = 'SET LOCAL vectors.enable_prefilter=on; SET LOCAL vectors.search_mode=vbase;';
 | 
					    if (numResults && numResults !== 100) {
 | 
				
			||||||
    if (numResults) {
 | 
					      return `SET LOCAL vectors.hnsw_ef_search = ${Math.max(numResults, 100)};`;
 | 
				
			||||||
      runtimeConfig += ` SET LOCAL vectors.hnsw_ef_search = ${numResults};`;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return runtimeConfig;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user