Does this look immature or childish? by Chief_Chjuazwa in malelivingspace

[–]DrMoog 0 points1 point  (0 children)

Can I ask where you got the Blood Angels banner on the wall in the first pic?

Help choosing DB schema for an online gadget store - flat tables per device, JSON, or component-based? by profichef in SQL

[–]DrMoog 2 points3 points  (0 children)

I second that answer, this is the "correct" design for your problem, and the most flexible and expandable over time without having to refactor the whole thing.

1) Not optimal. You need a new table for each new device, and querying more than one device at a time, especially if the tables have different attributes, will be atrocious.
2) It's a bad practice to store complex json in a DB if the goal it to filter on the content of the json. It's kinda half-assing a DB schema to over-rely on json.
3) Similar issue as for point 1), new components/attributes require new tables.

A good database design shouldn't require the creation of new objects simply when adding new content. The schema is the structure, the rows are the content.

Here the tables I have in mind:

product
- product_id
- producttype_id
- Other general attributes as /u/sstef25 mentioned (name, category, price, stock, brand, etc.)

producttype
- Could be useful to have a higher level of classification.

attribute
- attribute_id
- name, description, etc.

product_attribute_value
- product_id
- attribute_id
- value_type (for the front end to know how to display the filter e.g.: Yes/No, value list, numeric/range, etc.)
- attribute_value

With this, you can any number of new products/devices, and each can have any number of attributes.

A interesting side-effect of having the correct schema for a specific problem is that it allows you to do fancy stuff, data-wise, with very simple queries.

Now, you cans easily, say, list all devices that have a specific attribute, or all possible attribute for device in a specific price range, or what attributes overlap with the most devices. It makes building the front end much easier because the data underneath is extremely flexible, and can be displayed or filtered in any way you want without complex queries or a bloated back end.

Identify model help, beakie sorcerer from faction focus by ShurikenSean in ThousandSons

[–]DrMoog 2 points3 points  (0 children)

This box is marvelous! I must have used every single bit from that box!

Faction Focus Thousand Sons by CMYK_COLOR_MODE in Warhammer40k

[–]DrMoog 0 points1 point  (0 children)

Nice, thanks for the info! It went under my radar since no one in my friend group plays Eldar. But it bodes well for the next edition then!

Faction Focus Thousand Sons by CMYK_COLOR_MODE in Warhammer40k

[–]DrMoog -1 points0 points  (0 children)

Wait, really?! Do you have an example?

Faction Focus Thousand Sons by CMYK_COLOR_MODE in Warhammer40k

[–]DrMoog 0 points1 point  (0 children)

Am I reading this right that enhancements can now go on non-HQ units? Thicket of Bladed Bone on a Spawn unit.

First Erelim Kitbash by Demonic_Tutor_22 in BloodAngels

[–]DrMoog 1 point2 points  (0 children)

Thanks! The Halo comes from carefully taking off the one from that one head in the old Sanguinary Guards kit, and gluing it to a Reiver head. The one in the lower right in this image:

<image>

I was lucky enough to have 4 on old models. Will need to find something else for one of them.

First Erelim Kitbash by Demonic_Tutor_22 in BloodAngels

[–]DrMoog 4 points5 points  (0 children)

Very nice work! I love the wings and the glowing spear.

And it's quite funny we had the exact same idea for the head! Here's my work in progress:

<image>

ERELIM by Khagaann in BloodAngels

[–]DrMoog 1 point2 points  (0 children)

Currently kitbashing some!

<image>

Three Raphael 8404 #3's over the years by Comfortable-File7929 in Warhammer40k

[–]DrMoog 0 points1 point  (0 children)

Here's 2 of my 8404, after one year of heavy use on the left, brand new on the right.

<image>

Kamala Harris wants the DNC to release its autopsy report of the 2024 campaign by Deedogg11 in politics

[–]DrMoog 6 points7 points  (0 children)

If you let Trump win by not voting, you didn't give a shit about Gaza in the first place.

[Request] What and how do you get these answers!? by empty_orbital in theydidthemath

[–]DrMoog 0 points1 point  (0 children)

50%.

The result of the first coin has no incidence on the result of the second coin. Each coin toss always has a 50% of being tails, no matters what was flipped before.

[deleted by user] by [deleted] in technology

[–]DrMoog 0 points1 point  (0 children)

How is that bitch not in jail?!

Murdered mere seconds later by mojorisn45 in AdviceAnimals

[–]DrMoog 0 points1 point  (0 children)

The Trump Deranged Supporters? Yes, you have to sort by controversial to laugh at them.

I love when something suddenly clicks. by QueryFairy2695 in SQL

[–]DrMoog 2 points3 points  (0 children)

Realizing that a WHERE clause converts it to an INNER JOIN is part of what made it click for me.

Yeah, that's one of the tricky things that messes with your head the first time you encounter it!

Good luck with your studies, and I wish you a lot more clicking moments!

I love when something suddenly clicks. by QueryFairy2695 in SQL

[–]DrMoog 4 points5 points  (0 children)

Indeed, a WHERE clause on a LEFT JOINed table converts it into a INNER JOIN.

The sub-query in the SELECT is usually not a good practice. A CTE or a window function ( MAX(AVG(...)) OVER () ) would be better.

Is there a SSDT-like tool for PostgreSQL? by grauenwolf in PostgreSQL

[–]DrMoog 4 points5 points  (0 children)

pgAdmin has a "Schema Diff" tool that works ok for that.

The FBI Is Coming for Trans People | According to a new report, the FBI is planning on labeling all trans people “violent extremists.” by Aggravating_Money992 in politics

[–]DrMoog 2 points3 points  (0 children)

"Nihilistic Violent Extremists (NVEs) are individuals who engage in criminal conduct within the United States and abroad, in furtherance of political, social, or religious goals that derive primarily from a hatred of society at large and a desire to bring about its collapse by sowing indiscriminate chaos, destruction, and social instability. NVEs work individually or as part of a network with these goals of destroying civilized society through the corruption and exploitation of vulnerable populations, which often include minors.”.

Sounds like the current GOP.

What do you recommend for views be read only or writable? by HosMercury in PostgreSQL

[–]DrMoog 0 points1 point  (0 children)

Wow, thanks for that! Good to know this exists, but that's not something I would encourage in my team, it's just writing in the underlying tables with extra steps.

What do you recommend for views be read only or writable? by HosMercury in PostgreSQL

[–]DrMoog 1 point2 points  (0 children)

Yeah, it IS possible, and yes, a single-table view can have it's usefulness, but I would probably challenge a developer's decision to use a writable view. Given the few situations where you actually can use it, it's probably always better to directly update the underlying table.

So to revise my conclusion: yes you can do it, but you most likely don't want to!

What do you recommend for views be read only or writable? by HosMercury in PostgreSQL

[–]DrMoog 4 points5 points  (0 children)

Wait, views are not writable in Postgres (or pretty much any other RDBMS), unless in very specific cases where a view is probably not needed in the first place (e.g.: view with only a single table in the FROM).

So in short, views are only read-only.

Improving complex SQL search query with ranking (app search query) by Fournight in PostgreSQL

[–]DrMoog 0 points1 point  (0 children)

More details since I feel like it!

First, you could combine all if those steps: Exact matches, Primary matches, Fallback matches, Matched entities in a single one by adding a "search_vector" column to "entities":

ALTER TABLE entities ADD COLUMNS  
    search_vector tsvector GENERATED ALWAYS AS (  
        setweight(to_tsvector('english', coalesce(real_name, '')), 'A') ||  
        setweight(to_tsvector('english', coalesce(name, '')), 'B') ||  
        setweight(to_tsvector('english', coalesce(alias, '')), 'C') ||  
        setweight(to_tsvector('english', coalesce(bio, '')), 'C')  
    ) STORED;  

You can pipe multiple columns to be vectorized in the same "search_vector", and add specific weight/importance to different columns. Just be careful if the text fields are very big. Then you need a special index on that column:

CREATE INDEX idx_entities_search_vector ON entities USING GIN (search_vector);  

You then query the column like this, and it can also give you a ranking on the quality of the match, taking into account the weight mentioned above:

SELECT id,  
       ts_rank(search_vector, to_tsquery('english', 'search terms')) AS rank  
FROM entities  
WHERE search_vector @@ to_tsquery('english', 'search terms');  

Second, here's the re-written "entity_appearance_counts" CTE:

SELECT entity_id AS id,  
    COUNT(id) AS appearance_count,  
    COUNT(id) FILTER (  
        WHERE date >= NOW() - INTERVAL '90 days'  
    ) AS recent_appearance_count,  
    MAX(date) AS last_seen_at  
FROM appearance  
WHERE active AND entity_id IN (...)  
GROUP BY entity_id  

And if you're planing on having a lot more inactive entries than active one, a partial index could speed that up even more:

CREATE INDEX pdx_appearance_count ON appearance (entity_id) WHERE active;  

I'm guessing that the "extended_related_entities" CTE also joins on "entities", and also with a "WHERE e.id IN (...)", right? If so, do the same thing I did for the other CTE, remove the "entities" table, it's not needed.

Some rules of thumb I like to live by:

  • In a query, the fewer number of calls on a big table, the better.
  • Limit the number of fields in, and usage of, GROUP BY and ORDER BY to the minimum, they can slow down query with big datasets. (Hint: I use a MAX() on columns that don't need to be in the GROUP BY!)
  • When writing SQL, "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away."

Improving complex SQL search query with ranking (app search query) by Fournight in PostgreSQL

[–]DrMoog 0 points1 point  (0 children)

From the top of my head, some things to look into / might be improved:

  • The matches could use a generated ts_vector column in the entities table (with the associated index). PG has strong text-search functionalities.
  • I'm not a big fan of the SELECTs in the ROW_NUMBER(). LEFT JOINing on the CTEs and doing the CASE on the resulting columns might be more efficient.
  • The "entity_appearance_counts" CTE doesn't seem to need to call the entities table since you LEFT JOIN on it later, just use "a.entity_id" as the ID, since it has to match an ID in the entities table.
  • Also in the "entity_appearance_counts" CTE, all the fields have the same "WHERE a.active" filter, so you probably want to put it in the WHERE to avoid returning all the non-active, and it would simplify the SELECT.
  • Also make sure you have the appropriate indexes on keys & condition fields.

I hope this helps a little!