insert-update

How to improve INSERT performance on a very large MySQL table

浪子不回头ぞ 提交于 2019-11-28 00:39:37
问题 I am working on a large MySQL database and I need to improve INSERT performance on a specific table. This one contains about 200 Millions rows and its structure is as follows: (a little premise: I am not a database expert, so the code I've written could be based on wrong foundations. Please help me to understand my mistakes :) ) CREATE TABLE IF NOT EXISTS items ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(200) NOT NULL, key VARCHAR(10) NOT NULL, busy TINYINT(1) NOT NULL DEFAULT 1, created

Django - save() update on duplicate key

大憨熊 提交于 2019-11-27 19:18:11
问题 I have little application which allows a user to rate a video. The user can rate only once. So I have defined the uniqueness on the model. But he should be able change his rate. So the save() should update on duplicate key class VideoRate(models.Model): """Users can Rate each Video on the criterias defined for the topic""" user = models.ForeignKey(User) video = models.ForeignKey(VideoFile) crit = models.ForeignKey(VideoCrit) rate = models.DecimalField(max_digits=2, decimal_places=1, choices

Rails - User Input for Multiple models on a single form - How

好久不见. 提交于 2019-11-27 19:08:36
This is basically a nested form question, albeit with only one field that belongs to a parent model. My data entry form collects data for a model - however I also need to collect one other a data element/value (UserID) that actually goes into a parent record that will be created with the detail record. AFAIK Rails expects each form field to map to a model and I need to create an unbound data input field that I will use separately. How can I override this default behaviour and create a'free form/unbound field'? TIA, BC Heres something from my own app: Access it by: params[:company] and params[

How to get the original value of changed fields?

本小妞迷上赌 提交于 2019-11-27 18:00:20
问题 I'm using sqlalchemy as my orm, and use declarative as Base. Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) My question is, how do I know a user has been modified, and how to get the original values without query database again? user = Session.query(User).filter_by(id=user_id).first() # some operations on user .... # how do I know if the user.name has been changed or not? ... # How do get the original name?

How to INSERT a record or UPDATE if it already exists?

不打扰是莪最后的温柔 提交于 2019-11-27 14:22:54
问题 I have a table with columns record_id (auto inc), sender , sent_time and status . In case there isn't any record of a particular sender, for example "sender1", I have to INSERT a new record otherwise I have to UPDATE the existing record which belongs to "user1". So if there isn't any record already stored, I would execute # record_id is AUTO_INCREMENT field INSERT INTO messages (sender, sent_time, status) VALUES (@sender, time, @status) Otherwise I would execute UPDATE statement. Anyway..

Update a column in MySQL

情到浓时终转凉″ 提交于 2019-11-27 13:23:56
I have a table table1 with three columns and a bunch of rows: [key_col|col_a|col_b] I want to update col_a with a set of values (i.e. leaving col_b unchanged), something like this: INSERT INTO table1 AS t1 (key_col, col_a) VALUES ("k1", "foo"), ("k2", "bar"); But it doesn't work, how do I do this? Naveed You have to use UPDATE instead of INSERT: UPDATE Syntax For Example: UPDATE table1 SET col_a='k1', col_b='foo' WHERE key_col='1'; UPDATE table1 SET col_a='k2', col_b='bar' WHERE key_col='2'; UPDATE table1 SET col_a = 'newvalue' Add a WHERE condition if you want to only update some of the rows.

Is there possible to use createQueryBuilder for insert/update? If not, what function should I use?

主宰稳场 提交于 2019-11-27 12:28:02
问题 For now I succeded to create a function that retrieves data from the database using Doctrine's function createQueryBuilder. Does anybody know if there is a similar function to insert or update the database? Or how can i use createQueryBuilder? 回答1: Doctrine 2 ORM does not support INSERT via DQL or the DQL query builder. For a complete syntax, check the EBNF of DQL. To handle inserts in ORM, you always manually instantiate an entity and persist it with the entity manager: $user = new \My

Android/SQLite: Insert-Update table columns to keep the identifier

丶灬走出姿态 提交于 2019-11-27 11:37:45
问题 Currently, I am using the following statement to create a table in an SQLite database on an Android device. CREATE TABLE IF NOT EXISTS 'locations' ( '_id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' TEXT, 'latitude' REAL, 'longitude' REAL, UNIQUE ( 'latitude', 'longitude' ) ON CONFLICT REPLACE ); The conflict-clause at the end causes that rows are dropped when new inserts are done that come with the same coordinates. The SQLite documentation contains further information about the conflict

MySQL “INSERT … ON DUPLICATE KEY UPDATE” on Java: How to Differ Inserted/Updated/NoChange states

扶醉桌前 提交于 2019-11-27 09:47:57
I'm confused about the return value of MySQL's INSERT ... ON DUPLICATE KEY UPDATE statement. When I try it on a MySQL client (mysql terminal, phpmyadmin or MySQL Workbench), the execution results with one of the followings: 1 : If new record inserted (i.e. no duplicate key). 2 : If existing record updated in case of duplicate key. 0 : If update executed but no column value was changed. These results make sense. However, when I executed the same query in Java (with mysql-connector 5.1.34), JDBC's executeUpdate method returns 1 in case of both insertion and no change, and 2 in case of an

MySQL ON DUPLICATE KEY UPDATE while inserting a result set from a query

你离开我真会死。 提交于 2019-11-27 07:39:05
问题 I am querying from tableONE and trying to insert the result set into tableTWO. This can cause a duplicate key error in tableTWO at times. So i want to ON DUPLICATE KEY UPDATE with the NEW determined value from the tableONE result set instead of ignoring it with ON DUPLICATE KEY UPDATE columnA = columnA . INSERT INTO `simple_crimecount` (`date` , `city` , `crimecount`)( SELECT `date`, `city`, count(`crime_id`) AS `determined_crimecount` FROM `big_log_of_crimes` GROUP BY `date`, `city` ) ON