2

我遇到了一个烦人的问题。

我已经制作了一个系统,现在用户告诉我它正在向他们传递信息:

内存不足(需要 268435427 字节)

整个数据库大小为 12MB,出现问题的查询已经运行了好几个月,而且并没有那么复杂或很大。

数据库是innodb。我的服务器有 24GB 的内存,所以我严重怀疑它实际上内存不足。

my.cnf 如下:

key_buffer = 8000M

max_allowed_pa​​cket = 1M

table_cache = 2048M

排序缓冲区大小 = 1M

net_buffer_length = 1024M

读取缓冲区大小 = 1M

read_rnd_buffer_size = 24M

innodb_log_file_size = 5M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

innodb_buffer_pool_size = 1024M

innodb_additional_mem_pool_size = 2M

最大连接数 = 100

query_cache_size = 128M

query_cache_min_res_unit = 1024

query_cache_limit = 16MB

线程缓存大小 = 100

max_heap_table_size = 4096MB

在 Windows 任务管理器中查看时,我看到 18.8GB 可用,但只有 100MB 可用。它是Windows 2008 64bit Server,这可能是问题的根源吗?


这是查询:

$currency     = '(SELECT currencies.symbol
                            FROM parts_trading, currencies
                            WHERE parts_trading.enquiryRef = enquiries.id
                            AND parts_trading.sellingCurrency = currencies.id
                            LIMIT 1
                         )' ;

$amountDueSQL = '(
                         (SELECT SUM(quantity*(parts_trading.sellingNet
                                    +
                                    parts_trading.sellingVat))
                            FROM parts_trading
                            WHERE parts_trading.enquiryRef = enquiries.id
                         )
                         +
                         (SELECT SUM(enquiries_custom_fees.feeAmountNet
                                    +
                                    enquiries_custom_fees.feeAmountVat)
                            FROM enquiries_custom_fees
                            WHERE enquiries_custom_fees.enquiryRef = enquiries.id
                         )
                         )' ;
$amountPaidSQL = 'COALESCE(
                             (SELECT SUM(jobs_payments_advance.amount)
                                FROM jobs_payments_advance
                                WHERE jobs_payments_advance.jobRef = jobs.id
                             ),
                             0
                         )' ;

    $result = $dbh->prepare("SELECT SQL_CALC_FOUND_ROWS jobs.id, jobs_states.state, jobs.creationDate, users.username,
                            entity_details.name, enquiries.id as enquiryId, pendingCancelation,
                            IF(entity_details.paymentTermsRef = 1, # Outer IF condition
                               IF($amountDueSQL-$amountPaidSQL = 0.00, # Inner IF condition
                                  CONCAT('Paid in full (', $currency, $amountDueSQL, ')') # Inner IF TRUE
                               , # End of inner IF TRUE
                                  IF($amountPaidSQL > 0,
                                     CONCAT('Part paid (', $currency, $amountPaidSQL, ')'),
                                     'Unpaid'
                                  )
                               ) # End of inner IF

                            , # End of TRUE for outer IF
                               (SELECT entity_payment_terms.term
                                    FROM entity_details, entity_payment_terms
                                    WHERE entity_details.paymentTermsRef
                                          =
                                          entity_payment_terms.id
                                    AND entity_details.id = enquiries.entityRef
                               ) # End of FALSE for outer IF
                            ) AS payState, enquiries.orderNumber,

                            IF((SELECT COUNT(*)
                                    FROM invoices_out, (SELECT * FROM invoices_out_reference GROUP BY jobRef) AS tb1
                             WHERE invoices_out.id = tb1.invoiceRef
                             AND tb1.jobRef = jobs.id) > 1,
                               'Part-invoiced',
                               (SELECT invoices_out.date
                                    FROM invoices_out, (SELECT * FROM invoices_out_reference GROUP BY jobRef) AS tb1
                             WHERE invoices_out.id = tb1.invoiceRef
                             AND tb1.jobRef = jobs.id)
                             ) AS invoicedDate,

                            enquiries.id AS enquiryId,

                            IF((SELECT COUNT(*)
                                    FROM invoices_out, (SELECT * FROM invoices_out_reference GROUP BY jobRef) AS tb1
                             WHERE invoices_out.id = tb1.invoiceRef
                             AND tb1.jobRef = jobs.id) > 1,
                               'Multiple invoices',
                               (SELECT invoices_out.id
                                    FROM invoices_out, (SELECT * FROM invoices_out_reference GROUP BY jobRef) AS tb1
                             WHERE invoices_out.id = tb1.invoiceRef
                             AND tb1.jobRef = jobs.id)
                             ) AS invoiceNumber,

                            # If the state is 0 (i.e. they have an account, if true find out their payment terms, if false, instead reference the payment state directly.
                            (SELECT MAX(etaDate) FROM parts_trading WHERE parts_trading.enquiryRef = enquiries.id) AS maxEtaDate,
                            (SELECT COUNT(DISTINCT DATE(etaDate)) FROM parts_trading WHERE parts_trading.enquiryRef = enquiries.id) AS etaCounts, entity_credit_limits.creditLimit AS cLimit,
                            COALESCE((SELECT 
                                    SUM(qty*parts_trading_buying.buyingNet
                                    /
                                    (SELECT rateVsPound FROM currencies WHERE currencies.id = parts_trading_buying.buyingCurrency))
                                    FROM parts_trading_buying
                                    WHERE parts_trading_buying.enquiryRef = enquiries.id
                                    ), 0
                                    ) AS nonInvoicedBuyingCosts,
                            COALESCE((SELECT 
                                    SUM(feeAmountNet/(SELECT rateVsPound FROM currencies WHERE currencies.id = parts_trading_buying.buyingCurrency))
                                    FROM parts_trading_buying_charges, parts_trading_buying 
                                    WHERE parts_trading_buying_charges.partRef = parts_trading_buying.id
                                    AND parts_trading_buying.enquiryRef = enquiries.id
                                    ), 0
                                ) AS nonInvoicedBuyingFeeCosts,
                            (SELECT
                                SUM(quantity*parts_trading.sellingNet)
                                    /
                                    COALESCE(
                                        (SELECT invoices_out.rate
                                         FROM invoices_out, invoices_out_reference
                                         WHERE invoices_out.id = invoices_out_reference.invoiceRef
                                         AND invoices_out_reference.jobRef = jobs.id
                                         LIMIT 1),
                                        (SELECT rateVsPound
                                         FROM currencies
                                         WHERE currencies.id = parts_trading.sellingCurrency)
                                    )
                             FROM parts_trading
                             WHERE parts_trading.enquiryRef = enquiries.id
                            ) AS sellingParts,
                            COALESCE((SELECT
                                    SUM(enquiries_custom_fees.feeAmountNet)
                                    /
                                    COALESCE(
                                        (SELECT rate
                                         FROM invoices_out, invoices_out_reference
                                         WHERE invoices_out.id = invoices_out_reference.invoiceRef
                                         AND invoices_out_reference.jobRef = jobs.id LIMIT 1),
                                        (SELECT rateVsPound
                                         FROM currencies
                                         WHERE currencies.id = parts_trading.sellingCurrency
                                         )
                                    )
                                    FROM enquiries_custom_fees, parts_trading
                                    WHERE enquiries_custom_fees.enquiryRef = enquiries.id
                                    AND parts_trading.enquiryRef = enquiries.id), 0) AS sellingFees,
                            COALESCE((SELECT
                                    SUM(parts_shipping_out.shippingOutCost)
                                    FROM parts_shipping_out, parts_shipping_arrival_dates, parts_shipping_v2
                                    WHERE parts_shipping_out.arrivalsRef = parts_shipping_arrival_dates.id
                                    AND parts_shipping_arrival_dates.shippingRef = parts_shipping_v2.id
                                    AND parts_shipping_v2.jobRef = jobs.id
                                    ), 0
                                ) AS actualShippingOutFromEua,
                            (SELECT

                                SUM(quantity*parts_trading.sellingNet)
                                    /
                                    COALESCE(
                                        (SELECT invoices_out.rate
                                         FROM invoices_out, invoices_out_reference
                                         WHERE invoices_out.id = invoices_out_reference.invoiceRef
                                         AND invoices_out_reference.jobRef = jobs.id
                                         LIMIT 1),
                                        (SELECT rateVsPound
                                         FROM currencies
                                         WHERE currencies.id = parts_trading.sellingCurrency)
                                    )
                                +
                                COALESCE((SELECT
                                    SUM(enquiries_custom_fees.feeAmountNet)
                                    /
                                    COALESCE(
                                        (SELECT rate
                                         FROM invoices_out, invoices_out_reference
                                         WHERE invoices_out.id = invoices_out_reference.invoiceRef
                                         AND invoices_out_reference.jobRef = jobs.id LIMIT 1),
                                        (SELECT rateVsPound
                                         FROM currencies
                                         WHERE currencies.id = parts_trading.sellingCurrency
                                         )
                                    )
                                    FROM enquiries_custom_fees
                                    WHERE enquiries_custom_fees.enquiryRef = enquiries.id), 0)
                                -
                                COALESCE((SELECT 
                                    SUM(qty*parts_trading_buying.buyingNet
                                    /
                                    (SELECT rateVsPound FROM currencies WHERE currencies.id = parts_trading_buying.buyingCurrency))
                                    FROM parts_trading_buying
                                    WHERE parts_trading_buying.enquiryRef = enquiries.id
                                    ), 0
                                    )
                                -
                                COALESCE((SELECT 
                                    SUM(feeAmountNet/(SELECT rateVsPound FROM currencies WHERE currencies.id = parts_trading_buying.buyingCurrency))
                                    FROM parts_trading_buying_charges, parts_trading_buying 
                                    WHERE parts_trading_buying_charges.partRef = parts_trading_buying.id
                                    AND parts_trading_buying.enquiryRef = enquiries.id
                                    ), 0
                                )
                                -
                                COALESCE((SELECT
                                    SUM(parts_shipping_out.shippingOutCost)
                                    FROM parts_shipping_out, parts_shipping_arrival_dates, parts_shipping_v2
                                    WHERE parts_shipping_out.arrivalsRef = parts_shipping_arrival_dates.id
                                    AND parts_shipping_arrival_dates.shippingRef = parts_shipping_v2.id
                                    AND parts_shipping_v2.jobRef = jobs.id
                                    ), 0
                                )
                                FROM parts_trading, parts_trading_buying
                                WHERE parts_trading.enquiryRef = enquiries.id
                                AND parts_trading_buying.counterpartRef = parts_trading.id
                            ) AS margin
                            FROM jobs,
                            jobs_states, enquiries, users, jobs_payment_status, entity_details
                            LEFT JOIN entity_credit_limits ON entity_details.id = entity_credit_limits.entityRef
                            WHERE jobs.stateRef = jobs_states.id
                            AND IF(paymentStateRef = 0, 1, (jobs_payment_status.id = jobs.paymentStateRef))
                            # ^ If true it causes a result for each payment state (i.e. 3), so we group on state below, shouldn't cause probs.
                            AND jobs.enquiryRef = enquiries.id
                            AND enquiries.entityRef = entity_details.id
                            AND users.id = enquiries.traderRef
                            AND enquiries.traderRef = ?
                                LIMIT ?, ?") ;

如果我尝试将 PHP 执行内存设置为 3.5GB 以上,Apache 将无法启动(我使用的是 xampp)。我必须使用 32 位版本的 PHP?这与 INNODB_BUFFER_POOL_SIZE 相同,我希望为 14GB,但如果我这样做,mysql 将无法启动。

4

2 回答 2

1

我不认为这是与数据库相关的问题,因为这种错误(内存不足(需要 268435427 字节))大部分时间是由 PHP 抛出的(可能是无限循环或类似的问题)。

于 2012-07-05T07:28:11.500 回答
0

现在(花两分钟时间)您可以使用 MySQLCalculator.com 查看基本的 RAM 占用情况。如果配置中的所有每个连接 RAM 条目都有一个前导 # 你会运行得很好,实际上要好得多。使用 read_rnd_buffer_size = 24M 而不是默认的 256K 行,您读取的索引信息比必要的多 96 倍。

于 2018-02-08T20:45:31.087 回答