<table><tr><td style="">dvratil added a comment.
</td></tr></table><br /><div><div><blockquote style="border-left: 3px solid #a7b5bf; color: #464c5c; font-style: italic; margin: 4px 0 12px 0; padding: 4px 12px; background-color: #f8f9fc;"><p>[partTypeTable]</p>

<p>The Alternate Key is on (name, ns). Is it necessary to use both columns?</p></blockquote>

<p>Yes, type names have two parts: a <em>namespace</em> and a <em>name</em> and as such the combination of those two has to be unique, which is what the key enforces. <em>Namespace</em> determines the type of the part (generally it's either "PLD" for payloads or "ATR" for attributes) and <em>name</em> is the actual name of the type. The reason for the split is that sometimes you want to query for example all attributes belonging to an Item, in which case you can do a query like</p>

<div class="remarkup-code-block" style="margin: 12px 0;" data-code-lang="text" data-sigil="remarkup-code-block"><pre class="remarkup-code" style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; padding: 12px; margin: 0; background: rgba(71, 87, 120, 0.08);">SELECT * FROM PartTable LEFT JOIN PartTypeTable ON PartTypeTable.id = PartTable.partTypeId WHERE PartTypeTable.ns = 'ATR' AND PartTable.pimItemId = ?;</pre></div>

<blockquote style="border-left: 3px solid #a7b5bf; color: #464c5c; font-style: italic; margin: 4px 0 12px 0; padding: 4px 12px; background-color: #f8f9fc;"><p>Associative Tables do not have a Surrogate Key - slight anti-pattern</p></blockquote>

<p>What would be the benefit of having a Surrogate key for those relational tables? The reason for the composite PK is to enforce uniqueness of the value pairs in those tables, and from our point of view, the value pair is the unique identifier of the relation. There is no need to be able to refer to such relations by a single identifier.</p>

<blockquote style="border-left: 3px solid #a7b5bf; color: #464c5c; font-style: italic; margin: 4px 0 12px 0; padding: 4px 12px; background-color: #f8f9fc;"><p>What I recommend we (eventually) do is:</p>

<ul class="remarkup-list">
<li class="remarkup-list-item">Ensure we can drop the existing, composite Primary Key</li>
<li class="remarkup-list-item">Add a surrogate id to each table and make it the Primary Key</li>
<li class="remarkup-list-item">Create separate indexes on each Foreign Key</li>
</ul></blockquote>

<p>How about we only create separate indexes on each Foreign Key to make work easier for the planner, but we keep the composite PK (or turn it into a regular UNIQUE INDEX)? The alternative is introducing the surrogate id as a PK and adding a UNIQUE index on the Foreign Key pairs to enforce uniqueness but that seems like a lot of pointless overhead as the surrogate id would never be used and we would be introducing another index on potentially large tables.</p>

<blockquote style="border-left: 3px solid #a7b5bf; color: #464c5c; font-style: italic; margin: 4px 0 12px 0; padding: 4px 12px; background-color: #f8f9fc;"><p>[CollectionTable]<br />
 I would investigate further whether the index on (name, parentId) is necessary. It is possible it's necessary if we have an extremely high fan-out from the parent to the next level of children (e.g. 20, 50, 100 next-level children)?</p></blockquote>

<p>The index is UNIQUE, thus enforcing that we don't end up with multiple Collections with the same name and the same parent (same as you can't have multiple subfolders with the same name in one folder).</p>

<blockquote style="border-left: 3px solid #a7b5bf; color: #464c5c; font-style: italic; margin: 4px 0 12px 0; padding: 4px 12px; background-color: #f8f9fc;"><p>Generally indexes on low cardinality columns (e.g. gender) are not helpful. Unless of course one is fetching only by that column and perhaps a small percentage of the data qualifies by the value. With this in mind, I would investigate if we need indexes on enabled, syncPref, displayPref and indexPref. I suspect we should be able to drop them. I'm curious to know the SQL which uses these columns. I suspect other columns are used which would have existing supporting indexes.</p></blockquote>

<p>Usually, when client requests a Collection, those columns are used to filter the results by:</p>

<div class="remarkup-code-block" style="margin: 12px 0;" data-code-lang="text" data-sigil="remarkup-code-block"><pre class="remarkup-code" style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; padding: 12px; margin: 0; background: rgba(71, 87, 120, 0.08);">SELECT * FROM CollectionTable .... WHERE (CollectionTable.enabled = 1 OR CollectionTable.referenced = 1) AND ....</pre></div>

<p>Now that I think about it, the variance in those columns is rather low (all the *Pref columns usually have value of 2) in which case I guess the index is indeed not very useful. For the majority of users, even the "enabled" column is true everywhere.</p></div></div><br /><div><strong>TASK DETAIL</strong><div><a href="https://phabricator.kde.org/T7846" rel="noreferrer">https://phabricator.kde.org/T7846</a></div></div><br /><div><strong>To: </strong>pablo, dvratil<br /><strong>Cc: </strong>dvratil, kde-pim, pablo<br /></div>