How in terraform dynamodb specify composite primary key (hashkey) - database

My goal is to create terraform resource "aws_dynamodb_table" with composite primary key as it describes in AWS documentation.
Based on Terraform documentation it allows for hash_key to have only a single Attribute name. How can I have a hash_key made by multiple attributes?

Use the range_key terraform argument, which is the sort key on DynamoDB. That way you'll get a composite key as stated in the AWS documentation. The hash_key is referred as Partition key.
Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.
If you want to query among other attributes, try with a local_secondary_index.

Related

how to load de-normalised data using hazelcast?

We are using Hazelcast for in memory data grid. We want to extend it for analytic using in memory computation.I have few question regarding this
Which data structure to use ? (I do not have primary key as de-normalize table and have a huge data )
If IMap the only option then can we use composite key or dummy key which should have support for index and predicate?
This is not the right use case i.e Hazelcast can not used for analytics?
You can generate random keys based on UUID::randomUUID or you can create composite keys. Indexes can be created over values and keys (for keys use the magic keyword __key# and add the property of the key you're interested in.
Predicates use the same keyword if you're looking to run it against a composite key property, otherwise just query as you expect it from any other data.

In RDBMS, is the key used for building the index for a database always the primary key?

How do we specify which key is used for building the index for a
database in SQL?
In most if not all RDBMS, is the search key used for building the index for a database always
the primary key?
From Database Management Systems, 3rd Edition, by Raghu
Ramakrishnan, and Johannes Gehrke
In principle, we can use any key, not just the primary
key, to refer to a tuple. However, using the primary key is
preferable because it is what the DBMS expects - this is the
significance of designating a particular candidate key as a
primary key and optimizes for. For example, the DBMS may create
an index with the primary key fields as the search key, to make the
retrieval of a tuple given its primary key value efficient.
Thanks.
That depends on which RDBMS you are using. It will be something like
CREATE INDEX index_name ON table_name(key_name).
YES and NO.
a) If you are creating a table, and generally the RDBMS will create the index for this table using the primary key you specify in your CREATE TABLE statement. If you don't specify a primary key, RDBMS will help you choose an unique and non-null key, OR create an internal key (probably an int type) as primary key for this table.
b) Sometimes, according to the query pattern, you may find some keys other than primary key are used frequently (in where clause for example), then it is good to build new indexes using these keys.
There are two aspects to your question:
I'm trying to interpret your questions. Perhaps what you need to understand is that there can be more than one index into a table?
Let's say you have a Customers table with 3 columns, CustomerID, LastName, and FirstName.
You create an index using a specific CREATE INDEX or ALTER TABLE command where you specify the specific columns you want to have included in the index. You do not have to have the primary key as part of an index; for example, you may create an index on a table of customers by their last name and first name to speed name searches while still having a different primary key like customerID. Here's some SQL-like syntax.
CREATE INDEX customer_name_idx ON Customers(LastName, FirstName)
This index doesn't include any primary keys nor does it require a primary key to function properly. Internally, it will likely point to some internal row IDs that only the DBMS cares about.
I'm trying to understand what you mean here as well.
A DBMS can return a result regardless of the presence of an index; an index just makes it more efficient if your query matches up nicely with an index.
Designating a column as a primary key provides benefits such as enforced uniqueness, and possibly some performance benefits for enforcing other foreign key constraints.
As your quote says though, there is no written rule that says a primary key must also be an index. MySQL, and probably many other DMBSes, creates an index automatically on the table's primary key as it makes sense to do so from a technical level.
Anyway, I hope this makes sense and I hope I can clarify better if you have other questions.

How to create primary keys in ClickHouse

I did found few examples in the documentation where primary keys are created by passing parameters to ENGINE section.
But I did not found any description about any argument to ENGINE, what it means and how do I create a primary key.
Thanks in advance. It would be great to add this info to the documentation it it's not present.
Primary key is supported for MergeTree storage engines family.
https://clickhouse.tech/docs/en/engines/table_engines/mergetree_family/mergetree/
Note that for most serious tasks, you should use engines from the
MergeTree family.
It is specified as parameters to storage engine.
The engine accepts parameters: the name of a Date type column containing the date, a sampling expression (optional), a tuple that defines the table's primary key, and the index granularity.
Example without sampling support:
MergeTree(EventDate, (CounterID, EventDate), 8192)
Example with sampling support:
MergeTree(EventDate, intHash32(UserID), (CounterID, EventDate, intHash32(UserID)), 8192)
So, (CounterID, EventDate) or (CounterID, EventDate, intHash32(UserID)) is primary key in these examples.
When using ReplicatedMergeTree, there are also two additional parameters, identifying shard and replica.
https://clickhouse.tech/docs/en/engines/table_engines/mergetree_family/replication/#creating-replicated-tables
Primary key is specified on table creation and could not be changed later.
Despite the name, primary key is not unique. It just defines sort order of data to process range queries in optimal way. You could insert many rows with same value of primary key to a table.

Should I use composite primary keys if I use JPA and the keys can change?

I'm a little confused if I should use composite primary keys or surrogate keys.
Initially I wanted to use composite primary keys when they were needed so have a more clear database, but some parts of the keys could change and JPA doesn't allow to update an object's primary key.
For example I have a Product entity that has a primary key composed of a String code and another entity Account. The code can be updated, but with JPA I can only do that with JPQL which I'd like to avoid. Is it good in this case to use an auto incremented primary key?
Does this mean that the only solution is to use surrogate keys?
Thanks
As you said, according specification JPA does not support update of primary keys:
The application must not change the value of the primary key[10]. The
behavior is undefined if this occurs.[11]
...
[10] This includes not
changing the value of a mutable type that is primary key or an
attribute of a composite primary key.
[11] The implementation may, but
is not required to, throw an exception. Portable applications must not
rely on any such specific behavior.
Using surrogate key is in my opinion best option, most of the time also auto incremented one.
Other option would be removal of entity with original key and recreating it with new key (and other values from original entity). That is plenty of work, especially for entities with many relationships.

How to use a composite key with Castle ActiveRecord

I read that in order to map a class using Castle ActiveRecord
the class must have a primary key (surrogate or composite).
Now, suppose I have a table which I only want to use for reading
and the table doesn't have surrogate key or natural composite key.
Is there any way to be able to still have some manual Guid (or other id) generation
to make it possible to map the class?
Again, the table is used ONLY for reading purposes.
There is a good description of Castle Active Record with Composite Key here:
Active Records Primary Keys
Note using composite key is not recommended.
How do you plan to select rows from your table if you have no key?

Resources