WooCommerce商城开发:高性能订单存储数据库模式

这是一系列深入探讨的第一部分,专门用于解释高性能订单存储数据库模式的实施。

与1 月份提出的版本相比,数据库模式的变化很小。我们在不同的地方添加和删除了几列,但整体表结构与第一个提案中描述的相同:

我们在此项目中添加了4 个表:

  1. 主订单表
  2. 订单地址表
  3. 订单操作表
  4. 订单元表

该解决方案应该可以显着加快数据写入和读取速度:以前,每个新订单都需要对表中的记录进行一次 INSERT posts + 对每条postmeta记录进行近 40 个 INSERT。新的数据结构最多需要 5 个 INSERT。 

对多订单搜索,我们不得不多次将postmeta表与posts表连接起来(例如,跨地址字段和客户电子邮件地址进行搜索)。现在,搜索需要更少的连接,并且连接的表与postmeta表相比要小得多。

目录  隐藏 

主订单表

代码

订单地址表

代码

订单操作表

代码

订单元数据表

代码

关于订单的注意事项

订购商品

主订单表

主订单表包含以前存储在表中的所有数据wp_posts,并为最常用的元值添加列。 

自上次提议以来,我们删除了post_id列,因为wc_order.id始终与相应的post.ID相匹配,因此post_id不再需要。

我们还添加了:

  • type列以正确支持退款(这是一种订单类型,建模为父订单的子订单,就像以前的帖子一样)。 
  • customer_note用于存储客户可以在结帐过程中留下的注释的列
Column NameTypeDescriptionExample values
idbigint(20) unsignedUnique ID for order.1, 5, 143
statusvarchar(20)Status for the order.‘wc-processing’, ‘wc-completed’, ‘wc-refunded’
currencyvarchar(10)Currency identifier in which payment was received.‘GBP’, ‘USD’, ‘ZAR’
typevarchar(20)‘shop_order’, ‘shop_order_refund’
tax_amountdecimal(26,8)Total order tax amount12.45, 5.0
total_amountdecimal(26,8)Total order total, including tax15.66, 10.0
customer_idbigint(20) unsignedCustomer ID (0 in case of guest checkout)1, 5, 12
billing_emailvarchar(320)Billing email for customer‘john@doe.com’
date_created_gmtdatetimeTimestamp when order was created‘2021-12-31 23:59:59.999999’, ‘2022-04-31 23:59:59’
date_updated_gmtdatetimeTimestamp when the order or any of its properties were last updated‘2021-12-31 23:59:59.999999’, ‘2022-04-31 23:59:59’
parent_order_idbigint(20) unsignedOrder ID of the parent, in case this order is a refund
payment_methodvarchar(100)Identifier of payment method‘bacs’, 
payment_method_titletextTitle of payment method used‘Direct Bank Transfer’, 
transaction_idvarchar(100)Transaction ID provided by the payment gateway.‘ch_1HbohKHMqnIERF7AtGikC32B’
ip_addressvarchar(100)IP address used to create the order‘1.2.3.4’
user_agenttextThe user-agent string of the web client that was used to create the order.‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36’, ‘Scrapy/2.6 (+https://scrapy.org)’
customer_notetextStores the customer note added during the checkout flow.‘Call me on my cell once you ship the widget please’

代码

CREATE TABLE $wpdb->prefix.'wc_orders' (
 
    id BIGINT(20) UNSIGNED,
    status VARCHAR(20) NULL,
    currency VARCHAR(10) NULL,
    type VARCHAR(20) NULL,
    tax_amount DECIMAL(26, 8) NULL,
    total_amount DECIMAL(26, 8) NULL,
    customer_id BIGINT(20) UNSIGNED NULL,
    billing_email VARCHAR(320) NULL,
    date_created_gmt DATETIME NULL,
    date_updated_gmt DATETIME NULL,
    parent_order_id BIGINT(20) UNSIGNED NULL,
    payment_method VARCHAR(100) NULL,
    payment_method_title TEXT NULL,
    transaction_id VARCHAR(100) NULL,
    ip_address VARCHAR(100) NULL,
    user_agent TEXT NULL,
    customer_note TEXT NULL,
 
    PRIMARY KEY (id),
    KEY status (status),
    KEY date_created (date_created_gmt),
    KEY customer_id_billing_email (customer_id, billing_email),
    KEY billing_email (billing_email),
    KEY type_status (type, status),
    KEY parent_order_id (parent_order_id),
    KEY date_updated (date_updated_gmt)
);

订单地址表

订单地址表用于存储最终客户的帐单地址和送货地址。

我们从表中省略了date_created_gmt,因为该信息将存储在主订单表中。

Column NameTypeDescriptionExample Values
idbigint(20) unsignedUnique ID for order address record.1, 5
order_idbigint(20) unsignedOrder ID.3, 8
address_typevarchar(20)Type of address (billing, shipping, etc).‘billing’, ‘shipping’
first_nametextFirst name‘John’
last_nametextLast name‘Doe’
companytextCompany name‘John Doe Ltd.’
address_1textAddress line 1‘Oaktree rd.’
address_2textAddress line 2
citytextCity‘New York’
statetextState‘NY’
postcodetextPostcode/ZIP Code‘10019’, ‘E14 3QS’
countrytextCountry code from i18n/countries.php‘US’, ‘UK’
emailvarchar(320)Email‘john@doe.com’
phonevarchar(100)Phone number‘1-500-2345’

代码

CREATE TABLE $wpdb->prefix.'wc_order_addresses' (
     
    id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    order_id BIGINT(20) UNSIGNED NOT NULL,
    address_type VARCHAR(20) NULL,
    first_name TEXT NULL,
    last_name TEXT NULL,
    company TEXT NULL,
    address_1 TEXT NULL,
    address_2 TEXT NULL,
    city TEXT NULL,
    state TEXT NULL,
    postcode TEXT NULL,
    country TEXT NULL,
    email VARCHAR(320) NULL,
    phone VARCHAR(100) NULL,
 
    KEY order_id (order_id),
    UNIQUE KEY address_type_order_id (address_type, order_id),
    KEY email (email),
    KEY phone (phone)
);

订单操作表

Order Operational Table 存储我们用来维护内部订单状态的字段和标志。我们将这些字段与核心订单表分开,因为这些字段将来可能会根据内部实施细节进行更改。

在操作表中,有一列被重命名(download_permissions_granted被重命名为download_permmission_granted,注意权限中缺少s),还有一列被添加到recorded_sales,因为它存储在大多数订单中,但在原始提案中被省略了。

Column NameDetailDescriptionExample
idbigint(20) unsigned1, 5
order_idbigint(20) unsigned3, 8
created_viavarchar(100)The identifier for order creation source. WC core uses the following values:
– admin 
– checkout
– rest-api
– store-api
‘admin’, ‘checkout’
woocommerce_versionvarchar(20)WooCommerce version which was active when creating the order. Postmeta previously called `_order_version`‘4.6.0’, ‘6.7.0’
prices_include_taxtinyint(1)For internal use. Whether prices included taxes when purchased.0, 1
coupon_usages_are_countedtinyint(1)For internal use. Whether coupon usage is counted.0, 1
download_permission_grantedtinyint(1)Marks whether download permissions are granted to order or not.0, 1
cart_hashvarchar(100)Hash of cart contents, used when clearing cart when order payment is successful.‘010ae06d098de5f270bd27cc69721576’
new_order_email_senttinyint(1)Whether an email is sent for the order0, 1
order_keyvarchar(100)Key used to allow anonymous order access for payment and related operations.‘wc_order_jIe6po8pcXvZh’
order_stock_reducedtinyint(1)Whether the stock is reduced for order0, 1
date_paid_gmtdatetimeTimestamp when the order was first moved into any of the paid statuses.‘2021-12-31 23:59:59.999999’, ‘2022-04-31 23:59:59’
date_completed_gmtdatetimeTimestamp when the order was last moved into completed status.‘2021-12-31 23:59:59.999999’, ‘2022-04-31 23:59:59’
shipping_tax_amountdecimal(26,8)Total shipping tax12.34
shipping_total_amountdecimal(26,8)Total shipping amount, including tax45.69
discount_tax_amountdecimal(26,8)Total discount tax54.23
discount_total_amountdecimal(26,8)Total discount amount, including tax12.23
recorded_salestinyint(1)Stores information about whether sales were recorded.0, 1

代码

CREATETABLE$wpdb->prefix.'wc_order_operational_data'(

    id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARYKEY,

    order_id BIGINT(20) UNSIGNED NULL,

    created_via VARCHAR(100) NULL,

    woocommerce_version VARCHAR(20) NULL,

    prices_include_tax TINYINT(1) NULL,

    coupon_usages_are_counted TINYINT(1) NULL,

    download_permission_granted TINYINT(1) NULL,

    cart_hash VARCHAR(100) NULL,

    new_order_email_sent TINYINT(1) NULL,

    order_key VARCHAR(100) NULL,

    order_stock_reduced TINYINT(1) NULL,

    date_paid_gmt DATETIME NULL,

    date_completed_gmt DATETIME NULL,

    shipping_tax_amount DECIMAL(26, 8) NULL,

    shipping_total_amount DECIMAL(26, 8) NULL,

    discount_tax_amount DECIMAL(26, 8) NULL,

    discount_total_amount DECIMAL(26, 8) NULL,

    recorded_sales TINYINT(1) NULL,

    UNIQUEKEYorder_id (order_id),

    UNIQUEKEYorder_key (order_key)

);

订单元数据表

该表在功能上与wp_postmeta表类似,充当扩展数据的数据存储(除非扩展创建自己的表)。

与之前的提议相反,我们决定省略date_created_gmt,因为这些将存储在父订单中。

Column NameTypeDescriptionExample Values
idbigint(20) unsignedUnique ID for meta record.1, 5
order_idbigint(20) unsigned, indexedCorresponding order ID.3, 8
meta_keyvarchar(255)Name of the key.‘_stripe_customer_id’
meta_valuetextValue of the record.‘cus_ICD8cAbkdVKbZF’

代码

CREATETABLE$wpdb->prefix.'wc_orders_meta'(

    id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARYKEY,

    order_id BIGINT(20) UNSIGNED NULL,

    meta_key VARCHAR(255),

    meta_value TEXT NULL,

    KEYmeta_key_value (meta_key, meta_value(100)),

    KEYorder_id_meta_key_meta_value (order_id, meta_key, meta_value(100))

)

关于订单的注意事项

我们决定从项目范围中删除订单注释,因为我们认为它们不是大多数与订单相关的数据库操作的瓶颈。我们也不认为wp_comments当前存储它们的表通常会收到与订单备注竞争的大量写入流量。因为我们已经决定强制执行 posts 表中的订单 ID = 新表中的订单 ID 的不变性,所以我们不需要重新绑定订单注释以指向正确的订单。如果需要,我们计划根据进一步的性能测试和来自社区的反馈来处理订单备注。

订购商品

Order Items 已经有自己的专用表,它们超出了本项目的范围。

点击阅读 WooCommerce商城开发:高性能订单存储数据库模式 原文 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用WordPress开发商城是一个非常常见的选择,其中最受欢迎的插件就是WooCommerceWooCommerce不仅易于安装和使用,而且具有丰富的功能,可以满足各种商城的需求。为了创建一个漂亮和功能强大的商城,我们可以选择使用预先设计好的商城模板。 选择商城模板时,有几个因素需要考虑。首先,要确保模板兼容最新版本的WordPressWooCommerce插件,以确保安全性和功能正常运行。其次,模板应具有响应式设计,以适应各种设备。这意味着商城能够在桌面、平板和移动设备上都有良好的浏览体验。 另一个重要因素是模板的外观和布局。商城模板应具有现代、专业和易于导航的外观。它应该提供吸引顾客的页面设计和用户友好的购物体验。商城模板通常会提供多种页面样式,例如主页、产品目录、单品页和购物车页面等。 此外,模板应该支持自定义选项,以满足商城的个性化需求。这意味着我们可以根据自己的品牌和产品进行颜色、字体和布局的调整。模板还应该具备好的SEO(搜索引擎优化)特性,以便在搜索引擎中更好地排名,吸引更多的潜在客户。 最后,选择一个有良好技术支持和更新的商城模板也是非常重要的。这将确保我们在使用过程中能够得到及时的帮助和修复潜在的问题。 综上所述,在选择WordPress WooCommerce商城模板时,我们应该考虑兼容性、响应式设计、外观和布局、自定义选项、SEO特性以及技术支持和更新等因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值