Efficient way to search database rows from a large database table mysql - mysql

A my-sql database table is having millions of data records.This table consists of a primary key [say user id],serial number [can have duplicates] and some other columns which allows null values.
Eg: say the schema is
CREATE TABLE IF NOT EXISTS SAMPLE_TABLE (
USER_ID INTEGER NOT NULL,
SERIAL_NO NOT NULL,
DESCRIPTION VARCHAR(100),
PRIMARY KEY (USER_ID)
)ENGINE INNODB;
Now I want to search a data row,based on the serial number.
I tried first adding a unique index including both columns [user id and serial no.] as
CREATE UNIQUE INDEX INDEX_USERS ON U=SAMPLE_TABLE (USER_ID,SERIAL_NO);
and then search for the data query based on serial number as below;
SELECT * FROM SAMPLE_TABLE WHERE SERIAL_NO=?
But it didn't success and I'm getting OOM error in mysql server side when I execute above select query. Appreciate any help on this.

You should not have added user_id intobthecindex you created. You just need an index on serial_no for that query.

If you provides necessary codes,it would be better than given explainations..However first you should find the id references to seraial number,then search the column corresponding to id

Related

MySQL auto assign foreign key ID

I have a main table called results. E.g.
CREATE TABLE results (
r_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
r_date DATE NOT NULL,
system_id INT NOT NULL,
FOREIGN KEY (system_id) REFERENCES systems(s_id) ON UPDATE CASCADE ON DELETE CASCADE
);
The systems table as:
CREATE TABLE systems (
s_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
system_name VARCHAR(50) NOT NULL UNIQUE
);
I'm writing a program in Python with MySQL connector. Is there a way to add data to the systems table and then auto assign the generated s_id to the results table?
I know I could INSERT into systems, then do another call to that table to see what the ID is for the s_name, to add to the results table but I thought there might be quirk in SQL that I'm not aware of to make life easier with less calls to the DB?
You could do what you describe in a trigger like this:
CREATE TRIGGER t AFTER INSERT ON systems
FOR EACH ROW
INSERT INTO results SET r_date = NOW(), system_id = NEW.s_id;
This is possible only because the columns of your results table are easy to fill in from the data the trigger has access to. The auto-increment fills itself in, and no additional columns need to be filled in. If you had more columns in the results table, this would be harder.
You should read more about triggers:
https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html
https://dev.mysql.com/doc/refman/8.0/en/triggers.html

Cassandra + Mysql

hi i am new to Cassandra. I have little bit confusion in DB design in below scenario.
Currently i have 3 table : Post, User, PostLike.
Post : store post info
User : store user info
PostLIke :
CREATE TABLE PostLike (
like_time timestamp
post_id bigint,
user_id bigint,
PRIMARY KEY (like_time,post_id,user_id)
);
like_time : used to store post order by post like time.
cassandra provide this in OrderPreservingPartitioner
requirement is:
All Users Id which like a given post order by like_time and got them using :
select * from PostLike where post_id = ?
All posts liked by a user
select * from PostLike where user_id = ? : it gave error
[Invalid query] message="PRIMARY KEY column "post_id" cannot be
restricted (preceding column "ColumnDefinition{name=user_id,
type=org.apache.cassandra.db.marshal.LongType, kind=CLUSTERING_COLUMN,
componentIndex=0, indexName=null, indexType=null}" is either not
restricted or by a non-EQ relation)"
pls suggest what i need to do here :
need to Use MySQL with Cassandra for these relation
OR
Create 2 separate table in cassandra
CREATE TABLE PostLike (
like_time timestamp
post_id bigint,
PRIMARY KEY (like_date,post_id)
);
CREATE TABLE UserLike (
like_time timestamp
user_id bigint,
PRIMARY KEY (like_date,user_id)
);
or any other solution. Please help.
First of all, you are getting that error because you are specifying the second part of the primary key, without specifying the first part. When querying in Cassandra by a compound primary key, you cannot skip parts of the key. You can leave parts off of the end of the key (as in, just query by the partitioning key (see below), but it won't work if you try to skip parts of the key.
Next, secondary indexes do not work the same in Cassandra as they do in MySQL. In Cassandra, they are provided for convenience, and not for performance. The cardinality of post_id and user_id will likely be too high to be efficient. Especially in a large cluster with millions of rows, secondary index query performance will drop-off significantly on a high-cardinality secondary index.
The proper way to solve this, is to use your second option (as etherbunny recommended), but with a re-order of your primary keys.
CREATE TABLE PostLike (
like_time timestamp
post_id bigint,
PRIMARY KEY (post_id,like_date)
);
CREATE TABLE UserLike (
like_time timestamp
user_id bigint,
PRIMARY KEY (user_id,like_date)
);
The first key in a Cassandra primary key is known as the partitioning key. This key will determine which token range your row will be stored in.
The remaining keys in a Cassandra primary key are known as clustering columns. The clustering columns help to determine the on-disk sort order within a partitioning key.
That last part is important, as it (clustering order, as well as the ORDER BY keyword) behaves very differently from MySQL or any RDBMS. This way, if you SELECT * FROM user_like WHERE user_id=34574398 ORDER BY like_date you should see the likes for that user_id ordered by like_date. In fact, even without the ORDER BY clause, they should still be sorted by like_date. However, if you were to SELECT * FROM user_like ORDER BY like_date, your data would not sort in the expected order, because ordering only works when a partitioning key is specified.
Below Error resolve if i create index.
CREATE INDEX post_id_PostLike_indx ON post_like (post_id);
CREATE INDEX user_id_PostLike_indx ON post_like (user_id);
[Invalid query] message="PRIMARY KEY column "post_id" cannot be
restricted (preceding column "ColumnDefinition{name=user_id,
type=org.apache.cassandra.db.marshal.LongType, kind=CLUSTERING_COLUMN,
componentIndex=0, indexName=null, indexType=null}" is either not
restricted or by a non-EQ relation)"

sample sequence table in mysql

I have decided to use mysql sequence table, since I am using spring jdbc batch insert (can't get primary key with this feature), where I will be pass generated key while inserting each row, I have googled long time now, didnt get proper way of creating sequence table.
I have created a sequence table
create table table_sequence (value int not null) ENGINE = MYISAM;
but I feel it seems to be very basic, since I need to have max value, and cache limit for each instance.
I have many tables, do I need to have one sequence table for each table?
I have very less idea about db sequence, so suggestion are helpful to me. thanks
this may help you:
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;

MySQL auto increment based on field?

Is it possible to create auto-increment based on a specific field? For example i have UserId and Status fields, so for each row with same UserId i need to auto-increment its Status, not global.
There is three thing that come to mind when I read your question. One was an auto incrementing field which acts as your ID number. Updating a table with data that has no unique ID number. Searching for fields with the same Userid to Status
Mt First example is of a creating a table and your AUTO_INCREMENTing number ID:
CREATE TABLE tableNameHere
(
UniqueID int NOT NULL AUTO_INCREMENT,
FirstName varchar(255) NOT NULL,
StatusOrYourColumn int(100) NOT NULL,
PRIMARY KEY (UniqueUD)
)
More on auto incrementation.
You may have already built your table and now want to 'add' additional and or modify your fields using ALTER:
ALTER TABLE tableNameHere StatusOrYourColumn INT AUTO_INCREMENT PRIMARY KEY
But be careful, you don't want to overwrite your settings that you have already set.
Another Thing that came to my mind when reading was where you said Status and Userid where the same. You can find these using the WHERE clause like so:
SELECT * FROM tableName WHERE tableName.Userid = anotherTableOrTableName.Status
Using these queries you can update, remake, alter and query your database table.

Auto Index in My SQL

I am using MySQL for my database. I have a requirement to store a list of users in the system. The user will have both first name and last name.
I cannot have the first name and second name as the primary key. So I need a indexing key. But I do not want to enter the index value every time to add a new row.
I want the system to handle this with auto increment of previous value every time I try to add a new user. Please let me know how to do this.
Also I am supposed to do a search operation on the list of user with the first name. Please let me know the efficient way to do the search as the number of records could be more than a million.
create table users (
id int primary key auto_increment,
name varchar(64),
...
)
See here for more info
Add an INT() id column with auto_increment set. This way, the id column value will be incremented automatically on each INSERT.
To get a unique key without having to specify it, you need an auto_increment field:
create table people (
person_id int primary key auto_increment,
first_name varchar(50),
: : :
);
For efficiently searching first names, you just need an index on the first-name column. A couple of million rows is not a big table, so this should be reasonably efficient.
create index person_index using btree on people (first_name);