Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120574 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 35434 invoked from network); 14 Jun 2023 10:43:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Jun 2023 10:43:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C76141804D0 for ; Wed, 14 Jun 2023 03:43:52 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 14 Jun 2023 03:43:49 -0700 (PDT) Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-2b1a3fa2cd2so6993591fa.1 for ; Wed, 14 Jun 2023 03:43:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686739427; x=1689331427; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=+a9tUjGdE3MZ3kWRmfZ384FW/5mD/MUt+qTKhSgkv3o=; b=OYQ0TTmK9rzKqhhpinY2NVsRjqxtKfv+5HUaC3lvFM2cJCsgL8ZczKxL4/ommmKnag KOUCfQFKt8SBeG9/kjcy6mgbs/YgPscCc5ff2OgtiA3P4Gz2c1vX9HXyufxoms7jtoGw 40FOhHNbzMWeztZ1kBOPQ9mywT/kf8r8GV7qwm2IKHldfB0AM7vaquFAXB8nR/mByQCv 71Vf8Hc1g7+fR2WrSO6GkPih3/I177s1PDzYORXYcJ0Th19g4JXn9UKLu6ebY+3O0DGq 3VWwVZ2uFz6mADb4S48l4gl7/ZcYyQng4Fe0KXvkz/ZfLgLnTAIlin4eHi9+1LKwNnqs 9gaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686739427; x=1689331427; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+a9tUjGdE3MZ3kWRmfZ384FW/5mD/MUt+qTKhSgkv3o=; b=LjQLLVS/6HVUWFhz14nyNzvdm7GMrCTLHtXpVImHWQGgK+5GBdLuLMHdDgqaDXshGC Hug+zlDugLggVwokUfh3s45eJMo5hAXMI6AB4oUGbbJb5Pk0nK1jp8q9ZqvXwg7C7vqW sCtaTHr4rmYr0If0b8/J5lbY682Z7PkgYWyc9LAR6ttEIY0JY3KHPQC3VNH5tumjaRze U4E9hJ+/WLObr3LC4RhM2XdV6ncKNoZqfbxI7uCaIv+8nH5DlmTLmfG9KzemSMTH/KhW r5UKwRIsHZ6VHlQhx3ZvP4VE4lsWkzO86zyvs7iV16FAAQOgBkxFtbxtGGujWKsjeQxZ 8Vzw== X-Gm-Message-State: AC+VfDyhXZWDxEk8ldjNWGADPfN94Mky2Rj1MjljtTSf3aObCa3jwMuB x+mHMr3wF7ytWjnba0uatTZ0ddUZ54TpUhW5yWg= X-Google-Smtp-Source: ACHHUZ7e5lnmTzmdiOxVKNRJs90RVKjMj2QHGGp5SXKlWoiIRcAlQ0IDOKcELQSGOmnEFr7+/u8WJId5VB3GHAShxCQ= X-Received: by 2002:a2e:8786:0:b0:2ad:ac93:3c7d with SMTP id n6-20020a2e8786000000b002adac933c7dmr6346124lji.38.1686739427090; Wed, 14 Jun 2023 03:43:47 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 14 Jun 2023 12:43:36 +0200 Message-ID: To: Vinicius Dias Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000006164a205fe14a084" Subject: Re: [PHP-DEV] Possible RFC: PDOStatement::addBatch From: benjamin.morel@gmail.com (Benjamin Morel) --0000000000006164a205fe14a084 Content-Type: text/plain; charset="UTF-8" On Wed, 14 Jun 2023 at 01:41, Vinicius Dias wrote: > Hello, folks. I hope you all are well. > > I was talking to friend about a feature I would like to see in PHP but > I mentioned that unfortunately I do not have the knowledge to try > implementing it myself, since it is not something very simple, so he > mentioned that I could bring this discussion up to this list and see > if there would be a person willing to develop that. > > The feature is a new method in PDOStatement called addBatch to allow > batch INSERTs, exactly like Java's JDBC[1] has. This method would > allow us having one single INSERT SQL query, adding multiple values to > it, and sending this to the database at once, as one single query. > Hi Vinicius, For what it's worth, this is somehow possible in userland by using concatenated queries and PDOStatement::nextRowset(): ``` $statement = $pdo->prepare(' SELECT ?; SELECT ?, ?; SELECT ?, ?, ?; '); $statement->execute([ 'one', 'two', 'three', 'four', 'five', 'six' ]); do { print_r($statement->fetchAll(PDO::FETCH_ASSOC)); } while ($statement->nextRowset()); ``` Please note that this will only work with PDO::ATTR_EMULATE_PREPARES set to true (default value), so this is of (very) limited use, as it's most likely still sent to the db as individual queries. Plus it comes with potential caveats regarding concatenating queries, as implied by Kamil. If batching was native to PDO, and brought performance improvements (making less round trips to the database), I'd be all for it. In the meantime, if you just want to speed up consecutive INSERT statements, you might want to use extended inserts instead: INSERT INTO table (a, b) VALUES (1, 2), (3, 4), (5, 6); I have a library that abstracts this for you, if that helps: https://github.com/brick/db - Benjamin --0000000000006164a205fe14a084--