# Choosing optimal 'segment' size in PQ

**URL:** <https://forum.weaviate.io/t/choosing-optimal-segment-size-in-pq/1638>\
**Category:** Support\
**Created:** [March 6, 2024, 5:54pm UTC](https://forum.weaviate.io/t/choosing-optimal-segment-size-in-pq/1638 "2024-03-06T17:54:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jegadeesh](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@Jegadeesh](https://forum.weaviate.io/u/Jegadeesh)\
**Post date:** [March 6, 2024, 5:54pm UTC](https://forum.weaviate.io/t/choosing-optimal-segment-size-in-pq/1638/1 "2024-03-06T17:54:06Z")

</div>

In configuring PQ manually using the two phase method, the ‘segments’ parameter is to be configured.

Considering if the vector dimension to be 768. The possible segment values could be 384, 256, 192, 96. How to determine the best ‘segment’ value for configuring PQ manually?

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [March 6, 2024, 8:42pm UTC](https://forum.weaviate.io/t/choosing-optimal-segment-size-in-pq/1638/2 "2024-03-06T20:42:55Z")

</div>

Hi @Jegadeesh!

That’s a nice question.

I had to dig into our repo to find the answer 🤩

We have this issue where our core team discuss about it:

> <https://github.com/weaviate/weaviate/issues/3667>
>
> Currently when enabled PQ will set the default number of segments to be the numb…er of dimensions of the vectors. This is always guaranteed to work (as the # of segments must divide the # of dimensions), however it results in less memory savings for acceptable recall.
> 
> We should pick better defaults in an easier understandable way. Potentially something like this:
> 
> \`\`\`
> if dimensions \>= 768 and dimensions % 6 == 0
> segments = dimensions / 6
> else if dimensions \>= 512 and dimensions % 4 == 0
> segments = dimensions / 4
> else if dimensions \>= 64 and dimensions % 2 == 0
> segments = dimensions / 2
> else
> segments = dimensions
> \`\`\`
> 
> Abdel's experimental results showed \`dimensions / 6\` is best for Sphere/Ada embeddings (768, 1536) hence the first value. @abdelr @etiennedi do you have any comments on good defaults for the other dimension sizes? We should validate if a 1M MiniLM 384 dim dataset performs well with these defaults.

and finally this PR that was merged:

> <https://github.com/weaviate/weaviate/pull/3790>
>
> \### What's being changed:
> 
> When the PQConfig contains a zero value in the segm…ents parameter, it folds back to setting the default value. The default value used to be the dimensions. This is far from optimal. In some experiments we identified 6 as a good default number for this setting but still, it has to be an integer divisor of the dimensions. In this PR we set the segments to the closest integer divisor to 6 which is not bigger than 6.
> 
> \### Review checklist
> 
> \- \[\] Documentation has been updated, if necessary. Link to changed documentation:
> \- \[\] Chaos pipeline run or not necessary. Link to pipeline:
> \- \[X\] All new code is covered by tests where it is reasonable.
> \- \[\] Performance tests have been run or not necessary.

So, from the PR:

> When the PQConfig contains a zero value in the segments parameter, it folds back to setting the default value. The default value used to be the dimensions. This is far from optimal. In some experiments we identified 6 as a good default number for this setting but still, it has to be an integer divisor of the dimensions. In this PR we set the segments to the closest integer divisor to 6 which is not bigger than 6.

Let me know if that helps 🙂
