Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Alternative to ON DUPLICATE KEY UPDATE? #49

Open
vladd11 opened this issue May 5, 2022 · 4 comments
Open

Feature Request: Alternative to ON DUPLICATE KEY UPDATE? #49

vladd11 opened this issue May 5, 2022 · 4 comments
Labels
area/queryprocessor Query processor issues and requests

Comments

@vladd11
Copy link

vladd11 commented May 5, 2022

Hello.
I'd like to rewrite simple MySQL query:

INSERT INTO users(id, phone, sms_code, sms_code_expiration)
VALUES (?id, ?phone, ?sms_code, ?sms_code_expiration)
ON DUPLICATE KEY
UPDATE sms_code=?sms_code, sms_code_expiration=?sms_code_expiration;

I see that I need to execute 2 (or 1) requests to YDB (and use more Request Units) and I also need to handle exceptions on DB client.
Pseudocode:

try:
    """
    DECLARE $id AS String;
    DECLARE $sms_code AS Uint32;
    DECLARE $sms_code_expiration AS Datetime;
    DECLARE $phone AS Utf8;

    INSERT INTO users(id, phone, sms_code, sms_code_expiration)
    VALUES ($id, $phone, $sms_code, $sms_code_expiration);
    """
except PreconditionFailed:
    """
    DECLARE $phone AS Utf8;
    DECLARE $sms_code AS Uint32;
    DECLARE $sms_code_expiration AS Datetime;

    UPDATE users 
    SET sms_code=$sms_code, sms_code_expiration=$sms_code_expiration
    WHERE phone=$phone;
    """

And I also need to make phone a primary key (because there aren't UNIQUE fields).
But it's ok for me, if I want to change phone I can just remove field and add it again leaving previous ID.
Yes, there are any duplicate chance on ID field (it's used to references) but it's pretty small (due to UUID4).

@dcherednik
Copy link
Member

Hello.
Probably UPSERT can help with your task. https://ydb.tech/en/docs/yql/reference/syntax/upsert_into

@vladd11
Copy link
Author

vladd11 commented May 5, 2022

Hello. Probably UPSERT can help with your task. https://ydb.tech/en/docs/yql/reference/syntax/upsert_into

No, it can't.

UPSERT INTO users(id, phone, sms_code, sms_code_expiration)
VALUES (?id, ?phone, ?sms_code, ?sms_code_expiration);

will update id column of row, but I need to do this only if row doesn't exist.

@fomichev3000
Copy link
Member

UPSERT INTO users(id, phone, sms_code, sms_code_expiration)
VALUES (?id, ?phone, ?sms_code, ?sms_code_expiration);

will update id column of row, but I need to do this only if row doesn't exist.
Technically, updating id with the same value leads to just updating values. To my mind result would be the same, either you call INSERT INTO ... ON DUPLICATE KEY <update all fields> or UPSERT ... <specify all fields>. Do you see any controversial example?

@fomichev3000 fomichev3000 added the area/queryprocessor Query processor issues and requests label May 18, 2022
@uh-zuh
Copy link

uh-zuh commented Jul 28, 2022

INSERT INTO table1 (id, value, create_time)
VALUES ($id, $value, $create_time)
ON DUPLICATE KEY
UPDATE value = $value;

but UPSERT will modify create_time too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/queryprocessor Query processor issues and requests
Projects
None yet
Development

No branches or pull requests

4 participants