简体   繁体   中英

SQL: Get all rows with matching string, how can I improve performance?

I'm building a little application that allows users to create comments on webpages. The way it works is that you send the origin with the comment you want to add to a page, then you fetch all the comments for a page by querying WHERE origin LIKE 'https://somesite.com/page-1' .

id is unique, but origin obviously isn't, as there might be multiple comments for a specific page.

 table_name | column_name |          data_type
------------+-------------+-----------------------------
 Comment    | id          | text
 Comment    | origin      | text
 Comment    | body        | text

So far this has worked pretty well, but the database is still relatively small. I don't see this scaling very well but I'm unsure of how it could be improved.

My initial thought was to create a different copy of the above table on the fly for each new site added, but that sounds like a nightmare to manage in case I needed to deploy changes to my schema.

How should I handle a situation like this? How should my database schema look?

This condition:

WHERE origin LIKE 'https://somesite.com/page-1'

is equivalent to:

WHERE origin = 'https://somesite.com/page-1'

For equality, you can use a regular, B-tree index on origin :

CREATE INDEX idx_comment_origin ON comment(origin);

For more complex comparisons using LIKE , you can use a GIN index , but that is not necessary for the example in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM